The situation is simple. There is a method GetData. It gets all the data from the database and returns it.

public List<UserData> GetData(int FL) { string sql = string.Format(@"SELECT id, guid, username, userblob, ""FL"" FROM ""USERS"" WHERE ""FL""={0};", FL); ... UserData ud = new UserData(); ud.Id = dr[0].ToString(); ud.Guid = (dr[1].ToString()); ud.Name = (dr[2].ToString()); ud.UserBlob = (byte[])dr[3]; ud.FL = dr[4].ToString(); uds.Add(ud); ... return uds; } 

However, I had the need to call it by passing it another SQL SELECT WHERE IN command. I cannot pass it as a string. My database is defined as an abstraction:

  TargetDbContext = DbContextFactory.GetDbContext(_config.FirstDataBase); TransitDbContext = DbContextFactory.GetDbContext(_config.TransitDataBase); 

Just for each of the used databases, its own method is implemented: GetData and I would like to determine the request itself in the method itself, and not pass it outside. How can this be done?

    1 answer 1

    Try passing a flag instead of a query string.

    Create enum Query {Q1, Q2, Q3};

     public List<UserData> GetData(int FL, Query q) { switch (q) { case Q1: { string sql = string.Format(@"SELECT id, guid, username, userblob, ""FL"" FROM ""USERS"" WHERE ""FL""={0};", FL); ... UserData ud = new UserData(); ud.Id = dr[0].ToString(); ud.Guid = (dr[1].ToString()); ud.Name = (dr[2].ToString()); ud.UserBlob = (byte[])dr[3]; ud.FL = dr[4].ToString(); uds.Add(ud); ... return uds; break; } case Q2: { string sql = string.Format("что-то другое"); ... UserData ud = new UserData(); ud.Id = dr[0].ToString(); ud.Guid = (dr[1].ToString()); ud.Name = (dr[2].ToString()); ud.UserBlob = (byte[])dr[3]; ud.FL = dr[4].ToString(); uds.Add(ud); ... return uds; break; } } } 

    When calling a function, pass the flag, and then the correct part of the code will be executed.

    • This is a dead end road. For every sneeze, you have to create a new element enum , and redo the code in all GetData . - Igor
    • What is the way out? - Dmitry Bubnenkov
    • @Igor as I understand it - in the author 2 DB and 2 GetData . So the option is quite working in this situation - Yurii Manziuk
    • Well, yes, it seems to come down. The question is only. How to pass into the definition of the interface that it should take? `List <UserData> GetData (int FL, Query <T>);` Or how is this done in C #? - Dmitry Bubnenkov
    • He simply writes to me that img.ctrlv.in/img/16/05/18/573c7609d1ac9.png is true, I haven’t yet done sending the Table in the methods themselves. Maybe he hints about it? - Dmitry Bubnenkov