private ISessionFactory BuildSessionFactory(global::NHibernate.Cfg.Configuration config, IDomainModuleGroup group) { SessionFactoryBuilder builder = new SessionFactoryBuilder(); IEnumerable<INHDomainModule> modules = group.Modules.OfType<INHDomainModule>(); modules.SelectMany(m => m.BuildListeners).ForEach(builder.RegisterBuildListener); config.Properties["connection.connection_string"] = GetConnectionString(group); return builder.Build(config); }
public ISessionFactory Get(IDomainModuleGroup domainModuleGroup) { return GetByGroup(domainModuleGroup); }
private ISessionFactory BuildSessionFactory(IDomainModuleGroup group) { #if DEBUG Stopwatch stopwatch = new Stopwatch(); stopwatch.Start(); #endif global::NHibernate.Cfg.Configuration config; ISessionFactory sessionFactory; if (WarmStartCacheManager.IsUse) { WarmStartCacheManager.Reset(); WarmStartCacheManager.SetIdentifier(group.Name); var dependentAssemblies = group.Modules .SelectMany(g => g.AssemblyDependencies) .Union(new [] { GetType().Assembly, typeof(IDomainModule).Assembly } ) .Distinct(); WarmStartCacheManager.SetDependentAssemblies(dependentAssemblies); } if (WarmStartCacheManager.IsUse && WarmStartCacheManager.IsAbleToWarmStart) { Log.Debug(this, "Warm start method used."); config = WarmStartCacheManager.GetConfigFromCache(); sessionFactory = config.BuildSessionFactory(); } else { config = new global::NHibernate.Cfg.Configuration(); sessionFactory = BuildSessionFactory(config, group); } Configurations.Add(sessionFactory, config); if (WarmStartCacheManager.IsUse && !WarmStartCacheManager.IsAbleToWarmStart) { Log.Debug(this, "Config cached for warm start use."); WarmStartCacheManager.CacheConfig(config); } #if DEBUG stopwatch.Stop(); Log.Debug(this, string.Format("`{0}` session took {1:c}` seconds to build.", group.Name, stopwatch.Elapsed)); #endif return sessionFactory; }
protected string GetSessionKey(IDomainModuleGroup group) { return string.Format("NHibernateSessionFactory.{0}", group.Name); }
protected string GetConnectionStringKey(IDomainModuleGroup group) { return string.Format("NHibernate.{0}", group.Name); }
protected virtual string GetConnectionString(IDomainModuleGroup group) { var connectionStringEntry = ConfigurationManager.ConnectionStrings[GetConnectionStringKey(group)]; if (connectionStringEntry == null) { throw new Exception(string.Format("Cannot build ISessionFactory for group `{0}` because not connection string matching key `{1}` was found. Please add a connection string for and try again.", group.Name, GetConnectionStringKey(group))); } return connectionStringEntry.ConnectionString; }
protected ISessionFactory GetByGroup(IDomainModuleGroup group) { var key = GetSessionKey(group); if (!SessionFactories.ContainsKey(key)) { SessionFactories.Add(key, BuildSessionFactory(group)); } return SessionFactories[key]; }