Parameter Based Load Load

I have this problem:

In C #, I create a program that needs to connect to various databases (SQLite, SQL Server Compact, MS Access, etc.). I set the parameter called "dbType" in app.config (it can be 1 for SQLlite, 2 for SQL Server Compact, 3 for MS Access, etc.). This parameter must be changed by the user while the program is running with a drop-down menu or something like this.

Then the program reads this parameter and creates an instance of the database interface implementation (database), which corresponds to the selected database.

The code:

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));
                    break;

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

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

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

            return objectDb;
        }   

    }

Well, it looks like it works, but I would like to know if it is possible to simplify adding other databases, I mean, if there is another db, I have to change this class, recompile, etc.

BLL, , Person, Customer ..? , .

,

+3
4

MEF... , . IDataBaseImplemention, , , , MEF "" .

MEF .Net framework ( V4), , .

+1

, - , - . -.

, DbProviderFactories, DbProviderFactory, DbConnection .. / System.Data.Common http://msdn.microsoft.com/en-us/library/9tahwysy.aspx

connectionString , . http://msdn.microsoft.com/en-us/library/ms178411(v=VS.85).aspx

, "" , -. .

0

. - (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- - .

0

The most common way would be to force the user to specify the assembly of the qualified name of the type to be loaded. You can instantiate an object with its type name using something like this:

IDatabase GetDatabase(string typeName)
{
    var databaseType = Type.GetType(typeName);
    if (databaseType == null)
    {
        return null;
    }
    return (IDatabase)Activator.CreateInstance(databaseType);
}

This allows your users to precisely specify any implementation IDatabase- including those contained in assemblies provided by the user:

IDatabase db = GetDatabase("EnterpriseLib.XmlDatabase, EnterpriseLib.XmlDatabase, Version=1.42.0.0, Culture=neutral, PublicKeyToken=AAAAAAAAAAAAAAAA"");

This should work as long as the assembly is contained in one of the folders in the assembly search path.

0
source

Source: https://habr.com/ru/post/1793013/


All Articles