public DatabaseManager(DbProvider provider, string connectionString)
 {
     this.providerName = provider.GetDescription();
     this.connectionString = connectionString;
     this.connectionProvider = DbProviderFactories.GetFactory(providerName);
 }
        public NHUnitOfWorkFactory(
            DbProvider provider,
            string connectionString,
            string cacheProvider,
            Assembly[] mappingAssemblies)
        {
            NHUnitOfWorkFactory._dbProvider = provider;
            NHUnitOfWorkFactory.connectionString = connectionString;

            FluentConfiguration cfg = null;

            switch (_dbProvider)
            {
                case DbProvider.MsSqlProvider:
                    {
                        cfg = Fluently.Configure().Database(MsSqlConfiguration.MsSql2008
                                                            .Raw("format_sql", "true")
                                                            .ConnectionString(connectionString))
                              .ExposeConfiguration(
                                  c =>
                                  c.Properties.Add(
                                      Environment.SqlExceptionConverter,
                                      typeof(SqlExceptionHandler).AssemblyQualifiedName))
                              .ExposeConfiguration(c => c.Properties.Add(Environment.DefaultSchema, "dbo"));

                        break;
                    }

                case DbProvider.SQLiteProvider:
                    {
                        cfg = Fluently.Configure().Database(SQLiteConfiguration.Standard
                                                            .Raw("format_sql", "true")
                                                            .ConnectionString(connectionString));

                        inMemoryDatabase = connectionString.ToUpperInvariant().Contains(":MEMORY:");

                        break;
                    }

                case DbProvider.SqlCe:
                    {
                        cfg = Fluently.Configure().Database(MsSqlCeConfiguration.Standard
                                                            .Raw("format_sql", "true")
                                                            .ConnectionString(connectionString))
                              .ExposeConfiguration(
                                  c =>
                                  c.Properties.Add(
                                      Environment.SqlExceptionConverter,
                                      typeof(SqlExceptionHandler).AssemblyQualifiedName));

                        validationSupported = false;

                        break;
                    }

                case DbProvider.Firebird:
                    {
                        cfg = Fluently.Configure().Database(new FirebirdConfiguration()
                                                            .Raw("format_sql", "true")
                                                            .ConnectionString(connectionString));

                        break;
                    }

                case DbProvider.PostgreSQLProvider:
                    {
                        cfg = Fluently.Configure().Database(PostgreSQLConfiguration.PostgreSQL82
                                                            .Raw("format_sql", "true")
                                                            .ConnectionString(connectionString));

                        validationSupported = false;

                        break;
                    }
            }

            Guard.IsNotNull(
                cfg, $"Db provider {_dbProvider.GetDescription()} is currently not supported.");

            PropertyInfo pinfo = typeof(FluentConfiguration)
                                 .GetProperty(
                                     "Configuration",
                                     BindingFlags.Instance | BindingFlags.NonPublic);

            Configuration nhConfiguration = pinfo.GetValue(cfg, null) as Configuration;

            //IoC.RegisterInstance<Configuration>(nhConfiguration);

            cfg.Mappings(m =>
            {
                m.FluentMappings.Conventions.AddAssembly(typeof(NHUnitOfWorkFactory).Assembly);
                foreach (Assembly mappingAssembly in mappingAssemblies)
                {
                    m.FluentMappings.Conventions.AddAssembly(mappingAssembly);
                }
            })
            .Mappings(m =>
            {
                m.FluentMappings.AddFromAssembly(typeof(NHUnitOfWorkFactory).Assembly);
                foreach (Assembly mappingAssembly in mappingAssemblies)
                {
                    m.FluentMappings.AddFromAssembly(mappingAssembly);
                }
            })
            .Mappings(m =>
            {
                m.HbmMappings.AddFromAssembly(typeof(NHUnitOfWorkFactory).Assembly);
                foreach (Assembly mappingAssembly in mappingAssemblies)
                {
                    m.HbmMappings.AddFromAssembly(mappingAssembly);
                }
            })
            .ExposeConfiguration(c => c.Properties.Add(Environment.BatchSize, "100"))
            .ExposeConfiguration(c => c.Properties.Add(Environment.UseProxyValidator, "true"));

            if (!string.IsNullOrEmpty(cacheProvider))
            {
                cfg.ExposeConfiguration(c => c.Properties.Add(Environment.CacheProvider, cacheProvider))
                .ExposeConfiguration(c => c.Properties.Add(Environment.UseSecondLevelCache, "true"))
                .ExposeConfiguration(c => c.Properties.Add(Environment.UseQueryCache, "true"));
            }

            NHUnitOfWorkFactory.builtConfiguration = cfg.BuildConfiguration();
            NHUnitOfWorkFactory.builtConfiguration.SetProperty(
                Environment.ProxyFactoryFactoryClass,
                typeof(DefaultProxyFactoryFactory).AssemblyQualifiedName);
        }