/// <summary> /// Configures the factories. /// </summary> /// <param name="config">The config.</param> /// <param name="sessionFactoryResolver">The session factory resolver.</param> /// <param name="firstFactory">if set to <c>true</c> [first factory].</param> protected void ConfigureFactories(IConfiguration config, ISessionFactoryResolver sessionFactoryResolver, bool firstFactory) { String id = config.Attributes[SessionFactoryIdConfigurationKey]; if (string.IsNullOrEmpty(id)) { String message = "You must provide a " + "valid 'id' attribute for the 'factory' node. This id is used as key for " + "the ISessionFactory component registered on the container"; throw new ConfigurationErrorsException(message); } String alias = config.Attributes[SessionFactoryAliasConfigurationKey]; if (!firstFactory && (string.IsNullOrEmpty(alias))) { String message = "You must provide a " + "valid 'alias' attribute for the 'factory' node. This id is used to obtain " + "the ISession implementation from the SessionManager"; throw new ConfigurationErrorsException(message); } else if (string.IsNullOrEmpty(alias)) { alias = Constants.DefaultAlias; } string configurationBuilderType = config.Attributes[ConfigurationBuilderConfigurationKey]; string configurationbuilderKey = string.Format(ConfigurationBuilderForFactoryFormat, id); IConfigurationBuilder configBuilder; if (string.IsNullOrEmpty(configurationBuilderType)) { configBuilder = Kernel.Resolve<IConfigurationBuilder>(); } else { Kernel.Register(Component.For<IConfigurationBuilder>().ImplementedBy(Type.GetType(configurationBuilderType)).Named(configurationbuilderKey)); configBuilder = Kernel.Resolve<IConfigurationBuilder>(configurationbuilderKey); } var cfg = configBuilder.GetConfiguration(config); // Registers the Configuration object Kernel.Register(Component.For<NHibernate.Cfg.Configuration>().Instance(cfg).Named(String.Format("{0}.cfg", id))); // If a Session Factory level interceptor was provided, we use it if (Kernel.HasComponent(SessionInterceptorKey)) { cfg.Interceptor = (IInterceptor)Kernel[SessionInterceptorKey]; } // Registers the ISessionFactory as a component //var model = new ComponentModel(id, typeof(ISessionFactory), typeof(Empty)); //model.LifestyleType = LifestyleType.Singleton; //model.ExtendedProperties[Constants.SessionFactoryConfiguration] = cfg; //model.CustomComponentActivator = typeof (SessionFactoryActivator); //Kernel.AddCustomComponent(model); Kernel.Register(Component .For<ISessionFactory>() .Named(id) .Activator<SessionFactoryActivator>() .ExtendedProperties(Property.ForKey(Constants.SessionFactoryConfiguration).Eq(cfg)) .LifeStyle.Singleton); sessionFactoryResolver.RegisterAliasComponentIdMapping(alias, id); }
/// <summary> /// Configures the factories. /// </summary> /// <param name="config">The config.</param> /// <param name="sessionFactoryResolver">The session factory resolver.</param> /// <param name="firstFactory">if set to <c>true</c> [first factory].</param> protected void ConfigureFactories(NHibernateFactoryConfiguration config, ISessionFactoryResolver sessionFactoryResolver, bool firstFactory) { String id = config.Id; if (string.IsNullOrEmpty(id)) { const string message = "You must provide a " + "valid 'id' attribute for the 'factory' node. This id is used as key for " + "the ISessionFactory component registered on the container"; throw new ConfigurationErrorsException(message); } String alias = config.Alias; if (!firstFactory && (string.IsNullOrEmpty(alias))) { const string message = "You must provide a " + "valid 'alias' attribute for the 'factory' node. This id is used to obtain " + "the ISession implementation from the SessionManager"; throw new ConfigurationErrorsException(message); } if (string.IsNullOrEmpty(alias)) { alias = Constants.DefaultAlias; } string configurationBuilderType = config.ConfigurationBuilderType; string configurationbuilderKey = string.Format(ConfigurationBuilderForFactoryFormat, id); IConfigurationBuilder configBuilder; if (string.IsNullOrEmpty(configurationBuilderType)) { configBuilder = Kernel.Resolve <IConfigurationBuilder>(); } else { Kernel.Register( Component.For <IConfigurationBuilder>().ImplementedBy(Type.GetType(configurationBuilderType)).Named( configurationbuilderKey)); configBuilder = Kernel.Resolve <IConfigurationBuilder>(configurationbuilderKey); } var cfg = configBuilder.GetConfiguration(config.GetConfiguration()); // Registers the Configuration object Kernel.Register(Component.For <NHibernate.Cfg.Configuration>().Instance(cfg).Named(String.Format("{0}.cfg", id))); // If a Session Factory level interceptor was provided, we use it if (Kernel.HasComponent(SessionInterceptorKey)) { cfg.Interceptor = Kernel.Resolve <IInterceptor>(SessionInterceptorKey); } // Registers the ISessionFactory as a component Kernel.Register(Component .For <ISessionFactory>() .Named(id) .Activator <SessionFactoryActivator>() .ExtendedProperties(Property.ForKey(Constants.SessionFactoryConfiguration).Eq(cfg)) .LifeStyle.Singleton); sessionFactoryResolver.RegisterAliasComponentIdMapping(alias, id); }
private void ConfigureFactories(IConfiguration config, ISessionFactoryResolver sessionFactoryResolver, bool firstFactory) { String id = config.Attributes["id"]; if (id == null || String.Empty.Equals(id)) { throw new ConfigurationErrorsException("You must provide a " + "valid 'id' attribute for the 'factory' node. This id is used as key for " + "the ISessionFactory component registered on the container"); } String alias = config.Attributes["alias"]; if (!firstFactory && (alias == null || alias.Length == 0)) { throw new ConfigurationErrorsException("You must provide a " + "valid 'alias' attribute for the 'factory' node. This id is used to obtain " + "the ISession implementation from the SessionManager"); } else if (alias == null || alias.Length == 0) { alias = Constants.DefaultAlias; } if (config.Attributes["isWeb"] == "true") { appRootPath = HttpContext.Current.Server.MapPath("~/"); } else { appRootPath = Application.StartupPath; } NHibernate.Cfg.Configuration cfg = new NHibernate.Cfg.Configuration(); ApplyConfigurationSettings(cfg, config.Children["settings"]); RegisterAssemblies(cfg, config.Children["assemblies"]); RegisterResources(cfg, config.Children["resources"]); // Registers the Configuration object Kernel.AddComponentInstance(String.Format("{0}.cfg", id), cfg); // If a Session Factory level interceptor was provided, we use it if (Kernel.HasComponent("nhibernate.sessionfactory.interceptor")) { cfg.Interceptor = (IInterceptor)Kernel["nhibernate.sessionfactory.interceptor"]; } // Registers the ISessionFactory as a component ISessionFactory sessionFactory = cfg.BuildSessionFactory(); Kernel.AddComponentInstance(id, typeof(ISessionFactory), sessionFactory); // Registers the ISessionFactory within the ISessionFactoryResolver sessionFactoryResolver.RegisterAliasComponentIdMapping(alias, id); }
/// <summary> /// Configures the factories. /// </summary> /// <param name="config">The config.</param> /// <param name="sessionFactoryResolver">The session factory resolver.</param> /// <param name="firstFactory">if set to <c>true</c> [first factory].</param> protected void ConfigureFactories(IConfiguration config, ISessionFactoryResolver sessionFactoryResolver, bool firstFactory) { String id = config.Attributes["id"]; if (string.IsNullOrEmpty(id)) { String message = "You must provide a " + "valid 'id' attribute for the 'factory' node. This id is used as key for " + "the ISessionFactory component registered on the container"; throw new ConfigurationErrorsException(message); } String alias = config.Attributes["alias"]; if (!firstFactory && (string.IsNullOrEmpty(alias))) { String message = "You must provide a " + "valid 'alias' attribute for the 'factory' node. This id is used to obtain " + "the ISession implementation from the SessionManager"; throw new ConfigurationErrorsException(message); } else if (string.IsNullOrEmpty(alias)) { alias = Constants.DefaultAlias; } var configurationBuilder = Kernel.Resolve<IConfigurationBuilder>(); var cfg = configurationBuilder.GetConfiguration(config); // Registers the Configuration object Kernel.AddComponentInstance( String.Format("{0}.cfg", id), cfg ); // If a Session Factory level interceptor was provided, we use it if (Kernel.HasComponent("nhibernate.sessionfactory.interceptor")) { cfg.Interceptor = (IInterceptor) Kernel["nhibernate.sessionfactory.interceptor"]; } // Registers the ISessionFactory as a component var model = new ComponentModel(id, typeof(ISessionFactory), typeof(Empty)); model.LifestyleType = LifestyleType.Singleton; model.ExtendedProperties[Constants.SessionFactoryConfiguration] = cfg; model.CustomComponentActivator = typeof (SessionFactoryActivator); Kernel.AddCustomComponent(model); sessionFactoryResolver.RegisterAliasComponentIdMapping(alias, id); }
private void ConfigureFactories(IConfiguration config, ISessionFactoryResolver sessionFactoryResolver, bool firstFactory) { String id = config.Attributes["id"]; if (id == null || String.Empty.Equals(id)) { String message = "You must provide a " + "valid 'id' attribute for the 'factory' node. This id is used as key for " + "the ISessionFactory component registered on the container"; throw new ConfigurationErrorsException(message); } String alias = config.Attributes["alias"]; if (!firstFactory && (alias == null || alias.Length == 0)) { String message = "You must provide a " + "valid 'alias' attribute for the 'factory' node. This id is used to obtain " + "the ISession implementation from the SessionManager"; throw new ConfigurationErrorsException(message); } else if (alias == null || alias.Length == 0) { alias = Constants.DefaultAlias; } Configuration cfg = new Configuration(); ApplyConfigurationSettings(cfg, config.Children["settings"]); RegisterAssemblies(cfg, config.Children["assemblies"]); RegisterResources(cfg, config.Children["resources"]); // Registers the Configuration object Kernel.AddComponentInstance( String.Format("{0}.cfg", id), cfg ); // If a Session Factory level interceptor was provided, we use it if (Kernel.HasComponent("nhibernate.sessionfactory.interceptor")) { cfg.Interceptor = (IInterceptor) Kernel["nhibernate.sessionfactory.interceptor"]; } // Registers the ISessionFactory as a component ISessionFactory sessionFactory = cfg.BuildSessionFactory(); Kernel.AddComponentInstance( id, typeof(ISessionFactory), sessionFactory ); // Registers the ISessionFactory within the ISessionFactoryResolver sessionFactoryResolver.RegisterAliasComponentIdMapping(alias, id); }