Useful C# Generics

I've really been disappointed with C# generics, as an old C++ programmer I expected a similar implementation in C#, but that didn't happen. Today, I managed to write a class that used generics in a nice way. The end product works just like I want it to even though the syntax seems a little verbose.

I write a lot of database code and I seem to write the same pattern most of the time, something I'm really looking for LINQ to eliminate (or at least make less boring). I also know better than to commit to a single database (i.e. SQL vs. Access vs. ODBC). Here's a code snippet that illustrates how useful templates in C# are making my life easier.

public interface IDataProvider
{
    string ConnectionString { get; set; }
    DataSet Select(string query);
}

public class GenericDataProvider<ConnectionType, CommandType, DataAdapterType,
        CommandBuilderType> : IDataProvider
    where ConnectionType : System.Data.Common.DbConnection, new()
    where CommandType : System.Data.Common.DbCommand, new()
    where DataAdapterType : System.Data.Common.DbDataAdapter, new()
    where CommandBuilderType : System.Data.Common.DbCommandBuilder, new()
{
    private string m_cs;
    public string ConnectionString
    {
        get { return m_cs; }
        set { m_cs = value; }
    }

    public DataSet Select(string strSelect)
    {
        DataSet myDataSet = new DataSet();
        using (ConnectionType conn = new ConnectionType())
        {
            conn.ConnectionString = ConnectionString;
            CommandType command = new CommandType();
            command.CommandText = strSelect;
            command.Connection = conn;

            DataAdapterType adapter = new DataAdapterType();
            adapter.SelectCommand = command;

            conn.Open();
            adapter.Fill(myDataSet);
        }
        return myDataSet;
    }
}

Now I can create a couple of classes that do the real work.

public class SqlDataProvider : GenericDataProvider<SqlConnection,
   SqlCommand, SqlDataAdapter, SqlCommandBuilder> { }

public class AccessDataProvider : GenericDataProvider<OleDbConnection,
   OleDbCommand, OleDbDataAdapter, OleDbCommandBuilder> { }

About this entry