Click here to Skip to main content
15,886,806 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
I am trying to eliminate a bunch of duplicated code that follows this pattern:

C#
protected OdbcConnection Connection { get; set; }
protected  OdbcCommand Command { get; set;}
protected OdbcDataReader Reader { get; set; }

public string BuildQuery (string parm)
{
    string query = string.Empty;
    // build query
    return query;
}

public void ProcessDataFromReader()
{
    // do something with reader
}

public void GetData()
{
    Command.CommandText = BuildQuery("parms");

    using (Reader = Command.ExecuteReader())
    {
        while (Reader.Read())
        {
             ProcessDataFromReader();
        }
    }
}


I want to create a generic method that handles the methodology above but allow the two functions to be interchangable:
* ProcessDataFromReader()
* BuildQuery("parms")

Thanks

I am trying to build a generic method something like:
C#
public void GetData<T>(Func<object[], string> TQuery, Func<T> TRead)
{
    Command.CommandText = (string)TQuery();

    using (Reader = Command.ExecuteReader())
    {
	while (Reader.Read())
	{
	    TRead();
	}
    }
}


so I can have several queries to execute and be able to read the results of the queries without having to write the same pattern over and over.
Posted
Updated 11-May-15 10:28am
v2
Comments
virusstorm 11-May-15 15:48pm    
Are you able to provide an example of how you want to use the generic method? I'm having a hard time following what you want to achieve.
Sascha Lefèvre 11-May-15 16:18pm    
Same here. Not quite clear what you want.
Sergey Alexandrovich Kryukov 11-May-15 16:30pm    
So, what's the problem with using those functions? Where are the functions you would like to use? Are they generic or not? And so on...
—SA
tharper1977 11-May-15 16:41pm    
I would like to have multiple read queries (with parameter lists) and process results for the specific query. I am getting compile errors. I cannot seem to get the parameters to work.

1 solution

I think I have the solution:

C#
{
    public class Class1
    {

        protected OdbcConnection Connection { get; set; }
        protected OdbcCommand Command { get; set; }
        protected OdbcDataReader Reader { get; set; }

        public string BuildQuery1(string parm)
        {
            string query = string.Empty;
            // build query
            return query;
        }

        public string BuildQuery2(string parm1, double parm2)
        {
            string query = string.Empty;
            // build query
            return query;
        }

        public List<int> ProcessDataFromReader1()
        {
            // do something with reader
            return new List<int>();
        }

        public List<double> ProcessDataFromReader2()
        {
            // do something with reader
            return new List<double>();
        }

        public void GetGeneric()
        {
            Func<string> query = delegate { return this.BuildQuery1("parms"); };
            Func<List<int>> read = delegate { return this.ProcessDataFromReader1(); };
            GetData(query, read);
        }

        public void GetGeneric()
        {
            Func<string> query = delegate { return this.BuildQuery2("parms", 10d); };
            Func <List<double>> read = delegate { return this.ProcessDataFromReader2(); };
            GetData(query, read);
        }

        public void GetData<T>(Func<string> TQuery, Func<T> TRead)
        {
            Command.CommandText = TQuery();

            using (Reader = Command.ExecuteReader())
            {
                while (Reader.Read())
                {
                    TRead();
                }
            }
        }
    }
}
 
Share this answer
 
v2
Comments
Sascha Lefèvre 11-May-15 17:20pm    
ProcessDataFromReaderX() returning Lists when they're called for each row doesn't look right. And where do you return the final query result?
tharper1977 11-May-15 22:17pm    
The solution I put in is a mock of the real issue I am working on. The solution I came up with will get me in the right direction.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900