B
B
beduin012016-05-18 15:49:38
C++ / C#
beduin01, 2016-05-18 15:49:38

How to pass the request I need to the method?

The situation is simple. There is a GetData method. It gets all the data from the db 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 a need to call it by passing it another SQL `SELECT WHERE IN` command.
I can't pass it as a string. I have DB defined as an abstraction:
TargetDbContext = DbContextFactory.GetDbContext(_config.FirstDataBase);
TransitDbContext = DbContextFactory.GetDbContext(_config.TransitDataBase);

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

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
Anton Fedoryan, 2016-05-18
@AnnTHony

public List<UserData> GetData(int FL, string query)
{	
        switch (query)
        {
                case "WHERE":
                        // Делаем запрос WHERE
                       break;
                case "SELECT":
                        // Делаем запрос SELECT
                       break;
        }
    	return uds;
}

F
Fat Lorrie, 2016-05-18
@Free_ze

However, I have a need...

If this need is external, it is hardly manageable inside the method. You need to either add arguments, or notify the factory about it so that it returns the context with the correct GetData.

D
Dmitry Pavlov, 2016-05-18
@dmitry_pavlov

Sounds like the task of passing optional filters (optional parameters to a C# method).
But it is also possible to pass in general an external function as a parameter , which will be pulled by GetData for calculations unknown to it. Although, for me, the situation is more likely to signal an incorrectly designed code ...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question