So far my program only dealt with Sql Server for any type of data work. I would like my program to work with MySql as well. And now that I making this change, I would like to avoid code repetition as much as I can. So I started thinking about having a Factory Pattern with two concrete factories SqlServerFactory and MySqlFactory. This will work! But many of my database methods have pretty much the same structure except that in one, we are using an SqlConnection and SqlCommand and the other we are using a MySqlConnection and the rest of the method code is basically the same. Below is a example of one of my SELECT methods:
public DataTable GetSpyList()
{
DataTable dt = new DataTable();
using (OleDbConnection conn = new OleDbConnection(cs))
{
try
{
string query = "SELECT * FROM SpyList";
OleDbDataAdapter da = new OleDbDataAdapter(query, conn);
conn.Open();
da.Fill(dt);
conn.Close();
return dt;
}
catch (Exception ex)
{
return null;
}
}
}
How can I reduce code repetition by making the above method work for any type of data provider? Is this something that can be achieved by using DbProviderFactories?
Aucun commentaire:
Enregistrer un commentaire