Пример #1
0
        /// <summary>
        /// Настройка Nhibernate только с Fluent конфигураций.
        /// </summary>
        /// <param name="assemblies">Assemblies.</param>
        public static void ConfigureOrm(FluentNHibernate.Cfg.Db.IPersistenceConfigurer database, System.Reflection.Assembly[] assemblies, Action <Configuration> exposeConfiguration = null)
        {
            fluenConfig = Fluently.Configure().Database(database);

            fluenConfig.Mappings(m => {
                foreach (var ass in assemblies)
                {
                    m.FluentMappings.AddFromAssembly(ass);
                }
            });

            var trackerListener = new GlobalUowEventsTracker();

            fluenConfig.ExposeConfiguration(cfg => {
                cfg.AppendListeners(NHibernate.Event.ListenerType.PostLoad, new[] { trackerListener });
                cfg.AppendListeners(NHibernate.Event.ListenerType.PreLoad, new[] { trackerListener });
                cfg.AppendListeners(NHibernate.Event.ListenerType.PostDelete, new[] { trackerListener });
                cfg.AppendListeners(NHibernate.Event.ListenerType.PostUpdate, new[] { trackerListener });
                cfg.AppendListeners(NHibernate.Event.ListenerType.PostInsert, new[] { trackerListener });
            });

            if (exposeConfiguration != null)
            {
                fluenConfig.ExposeConfiguration(exposeConfiguration);
            }

            Sessions = fluenConfig.BuildSessionFactory();
        }
Пример #2
0
        /// <summary>
        ///     Principal método privado, realiza a criação do SessionFactory e este não deve ser criado novamente até que o domínio de aplicação seja finalizado.
        /// </summary>
        /// <returns></returns>
        protected override NH.ISessionFactory BuildSessionFactoryInternal()
        {
            FluentNH.Cfg.Db.IPersistenceConfigurer databaseConfiguration = this.BuildPersistenceConfigurer();

            FluentNH.Cfg.FluentConfiguration configuration = FluentNH.Cfg.Fluently
                                                             .Configure()
                                                             .Database(databaseConfiguration)
                                                             .Cache(it =>
                                                                    it.UseQueryCache()
                                                                    .ProviderClass <NH.Cache.HashtableCacheProvider>()
                                                                    )
                                                             .Diagnostics(it =>
                                                                          it.Enable(this.EnabledDiagnostics)
                                                                          .OutputToConsole()
                                                                          );


            if (this.TypeNames == null)
            {
                throw new NullReferenceException("TypeNames is not set");
            }

            foreach (string typeName in this.TypeNames)
            {
                Type typeInfo = Type.GetType(typeName);
                if (typeInfo == null)
                {
                    throw new ConfigurationErrorsException(string.Format(CultureInfo.InvariantCulture, "Cannot load the Type '{0}', defined in TypeNames property of FluentNHibernateSessionFactoryBuilder", typeName));
                }

                configuration.Mappings(it =>
                {
                    it.FluentMappings.AddFromAssembly(typeInfo.Assembly);
                    it.HbmMappings.AddFromAssembly(typeInfo.Assembly);
                });
            }

            configuration = configuration.ExposeConfiguration(it =>
            {
                NH.Cfg.Configuration config = it
                                              .SetProperty("command_timeout", this.CommandTimeout.ToString(CultureInfo.InvariantCulture))
                                              .SetProperty("adonet.batch_size", this.BatchSize.ToString(CultureInfo.InvariantCulture));

                if (this.NHibernateRawConfigurationValues != null && this.NHibernateRawConfigurationValues.Count > 0)
                {
                    foreach (System.Collections.Generic.KeyValuePair <string, string> rawConfigurationValue in this.NHibernateRawConfigurationValues)
                    {
                        config.SetProperty(rawConfigurationValue.Key, rawConfigurationValue.Value);
                    }
                }

                this.AddEventListeners(config);

                if (this.OnExposeConfiguration != null)
                {
                    this.OnExposeConfiguration(it);
                }
            });

            NH.ISessionFactory sessionFactory = configuration.BuildSessionFactory();
            return(sessionFactory);
        }