Exemple #1
0
        public static IDbConnection GetConnection(this IAmbiente ambiente)
        {
            /*
             * Assembly assembly = Assembly.LoadFrom("MyNice.dll");
             * Type type = assembly.GetType("MyType");
             * object instanceOfMyType = Activator.CreateInstance(type);
             */

            var pathDriverAssembly = Path.Combine("Drivers", ambiente.TipoDatabase.ToString());
            var pathDriver         = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, pathDriverAssembly);

            PathExtensions.SetPath(pathDriver);

            var nomeAssembly = Path.Combine(pathDriver, ambiente.TipoDatabase.GetNomeAssembly());
            var assembly     = Assembly.LoadFrom(nomeAssembly);

            var nomeType = ambiente.TipoDatabase.GetNomeType();
            var type     = assembly.GetType(nomeType);

            var connection = (IDbConnection)Activator.CreateInstance(type);

            connection.ConnectionString = ambiente.GetConnectionString();

            return(connection);
        }
Exemple #2
0
        public static DataBase CreateDataBase(IAmbiente ambiente)
        {
            if (sectionHandler?.Name?.Length == 0)
            {
                throw new Exception("Database name not defined in DbFactoryConfiguration section of config file");
            }

            try
            {
                Type            database        = Type.GetType(sectionHandler.Name);
                ConstructorInfo constructorInfo = database.GetConstructor(new Type[] { });
                DataBase        databaseObj     = (DataBase)constructorInfo.Invoke(null);
                databaseObj.connectionString = ambiente.GetConnectionString(sectionHandler.ConnectionString);
                return(databaseObj);
            }
            catch (Exception excep)
            {
                throw new Exception("Error instantiating database " + sectionHandler.Name + ". " + excep.Message);
            }
        }