Exemple #1
0
        public NHibernateUnitOfWorkFactory(
            DbProvider provider,
            string connectionString,
            string cacheProvider,
            Assembly[] mappingAssemblies)
        {
            _DbProvider       = provider;
            _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,
                string.Format(
                    "Db provider {0} is currently not supported.",
                    EnumExtension.GetEnumMemberValue(_DbProvider)));

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

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

            ServiceLocator.RegisterInstance <Configuration>(nhConfiguration);

            cfg.Mappings(m =>
            {
                m.FluentMappings.Conventions.AddAssembly(typeof(NHibernateUnitOfWorkFactory <TIdentifier>).Assembly);
                foreach (Assembly mappingAssembly in mappingAssemblies)
                {
                    m.FluentMappings.Conventions.AddAssembly(mappingAssembly);
                }
            })
            .Mappings(m =>
            {
                m.FluentMappings.AddFromAssembly(typeof(NHibernateUnitOfWorkFactory <TIdentifier>).Assembly);
                foreach (Assembly mappingAssembly in mappingAssemblies)
                {
                    m.FluentMappings.AddFromAssembly(mappingAssembly);
                }
            })
            .Mappings(m =>
            {
                m.HbmMappings.AddFromAssembly(typeof(NHibernateUnitOfWorkFactory <TIdentifier>).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"));
            }

            _builtConfiguration = cfg.BuildConfiguration();
            _builtConfiguration.SetProperty(
                Environment.ProxyFactoryFactoryClass,
                typeof(DefaultProxyFactoryFactory).AssemblyQualifiedName);

            #region Add Listeners to NHibernate pipeline....

            _builtConfiguration.SetListeners(
                ListenerType.Flush,
                new IFlushEventListener[] { new FixedDefaultFlushEventListener() });

            _builtConfiguration.SetListeners(
                ListenerType.FlushEntity,
                new IFlushEntityEventListener[] { new AuditFlushEntityEventListener <TIdentifier>() });

            _builtConfiguration.SetListeners(
                ListenerType.PreInsert,
                _builtConfiguration.EventListeners.PreInsertEventListeners.Concat(
                    new IPreInsertEventListener[] { new ValidateEventListener(), new AuditEventListener <TIdentifier>() }).ToArray());

            _builtConfiguration.SetListeners(
                ListenerType.PreUpdate,
                _builtConfiguration.EventListeners.PreUpdateEventListeners.Concat(
                    new IPreUpdateEventListener[] { new ValidateEventListener(), new AuditEventListener <TIdentifier>() }).ToArray());

            #endregion
        }