I've this issue:

In C# I am creating a program which should connect with various databases (SQLite, SQL Server Compact, MS Access, etc). I occur application.config a parameter known as "dbType" (it may be 1 for SQLlite, 2 for SQL Server Compact, 3 for MS Access, etc.). This parameter ought to be transformed through the user as the program is running, having a dropdown menu or something like that such as this.

Then your program reads this parameter and produces a clear case of an implementation from the database interface (IDatabase) that matches the selected database.

The code is:

class ObjectDatabaseCreator
        protected ObjectDatabase objectDb;
        protected Object objectDAO;
        protected int dbType;
        protected String dbName;

        public ObjectDatabaseCreator()

        public ObjectDatabaseCreator(String dbName)
            this.dbType = ObjectConfiguration.getDbType();
            this.dbName = dbName;

        public ObjectDatabase getObjectDatabase()
            //1 - SQLite; 2-SQLServer Compact; 3-SQLServer Express; 4-MS Access

            switch (dbType)

                case 1:
                    objectDb = new ObjectDatabase(new Database_Impl_1(dbName));

                case 2:
                    objectDb = new ObjectDatabase(new Database_Impl_2(dbName));

                case 3:
                    objectDb = new ObjectDatabase(new Database_Impl_3(dbName));

                case 4:
                    objectDb = new ObjectDatabase(new Database_Impl_4(dbName));

            return objectDb;


Well, it appears that actually works but I must determine if there's possible to really make it simpler to include another databases, I am talking about, if you will see another db I ought to modify these kinds, recompile, etc.

And just how could I instantiate even the other implementation for BLL classes, let us say Person, Customer, etc ? These also changes and that i should increase the classes.

Thanks, Trav

have a look at MEF... it may be helpful since it add the idea of catalog. Defines as numerous IDataBaseImplemention class as you would like in devices situated inside a folder, and MEF will help you to "import" all found classes.

MEF is incorporated within the .Internet framework (in the V4), but you will find a number of other framework that may get the job done.

I believe there's a problem along with you design if business classes change in line with the database engine getting used. And So I suggest normalizing your company classes.

In regards to the database connections, You ought to be while using DbProviderFactories, DbProviderFactory, DbConnection etc. in the System.Data.Common set up/namespace http://msdn.microsoft.com/en-us/library/9tahwysy.aspx

together with the connectionString portion of the config file if you wish to do that properly. http://msdn.microsoft.com/en-us/library/ms178411(v=VS.85).aspx

If you wish to just enhance your current implementation, then use Industrial facilities for the databaseobject in addition to business objects. However ,, this can just are more of an issue with time.

You can include a roadmap in the integer value to some class type. Something similar to this (untested):

// initialization somewhere:
Dictionary<int,Type> databases = new Dictionary<int,Type>();
databases.Add(1, Database_Impl_1);
databases.Add(2, Database_Impl_2);
// ...
// then later, in getObjectDatabase()
objectDb = new ObjectDatabase(databases[settingValue]);

Google inversion of control - you will find frameworks with this.