views:

90

answers:

2

Hi i have this class to instantiate DAL classes:

public class Factory
{
    public static T GetInstance<T>() where T : new()
    { 
        return new T();
    }
}

I want to make my application capable of using multiple databases. I was planning on setting the database in my web.config and then pass in that setting possibly to the factory class where it will return the correct DAL class. I think my methodology is ok im just a bit stuck on how to implement it whilst keeping it generic.

Maybe something like this:

public class Factory
{
    private static readonly string dbType = ConfigurationSettings.Appsettings["SqlServer"];
    public static T GetInstance<T>() where T : new()
    { 
        switch(dbType)
        {
             case "SqlServer":
                return new T(); //Not sure what to put here.
             break;
             case: "MySql":
                return new T(); 
             break;
             default: "No datasource";
        }
    }
}

If anyone could help or point me in the right direction that would be great.

Thanks in advance.

+3  A: 

Don't use generics on your GetInstance() method. Have all your data access classes implement an interface instead, and have that interface be the return value from the function.

public class Factory
{
    private static readonly string dbType = ConfigurationSettings.Appsettings["SqlServer"];
    public static IDataAccess GetInstance()
    { 
        switch(dbType)
        {
             case "SqlServer":
                return new SqlServerDataAccess(); //SqlServerDataAccess should implement IDataAccess
             break;
             case: "MySql":
                return new MySqlDataAccess(); //MySqlDataAccess should implement IDataAccess
             break;
             default: "No datasource";
        }
    }
}

Separate interfaces from implementation!

whatknott
+1 for recommending that it return an interface, -1 for reinventing the System.Data.Common wheel.
Steven Sudit
Thanks for the reply. Just curious why wouldn't you use generics? is it just over complicating things?
geepie
What are you trying to accomplish by using generics? If you use generics the caller has to tell the factory what concrete type it wants in return, when really the caller shouldn't care what implementation it gets in return.
whatknott
+2  A: 

You should look at the System.Data.Common NameSpace. This namespace uses structures like DBConnection, DBReader, and so on, and itself uses a factory method to great the required DbProvider.

So instead of going down your current path, I would suggest letting the current .net data framework do the lifting for you. here's a quick example.

DbProviderFactory m_factory = DbProviderFactories.GetFactory("System.Data.SqlClient");
    DbConnection m_connection = m_factory.CreateConnection();
    m_connection.ConnectionString = _connstrbldr.ConnectionString;
    m_connection.Open();
    using (DBCommand cmd = m_connection.CreateCommand())
    {
        cmd.CommandType = CommandType.Text;
        cmd.Commandtext = "";
        cmd.ExecuteNonQuery();
    }

The GetFactory call can accept any Provider that installed on the machine, Oracle, MySql, Sql, etc. You can also Get all providers that are installed on a machine by making a call to static DataTable GetFactoryClasses() this returns a datatable object.

The idea behind this would be to avoid provider specific implementations and rely on a generic implementation that would accommodate all your needs.

Writing Provider Independent Code in ADO.NET

I hope that you find this helpful.

Salizar Marxx
Thankyou very much, i will look into it. No point rolling my own if the functionality's already there.
geepie