Windsor + NHibernate + ISession + MVC

I am trying to get Windsor to provide me with an instance of ISession for every request that needs to be entered into all repositories.

Here is my container setup

container.AddFacility<FactorySupportFacility>().Register( Component.For<ISessionFactory>().Instance(NHibernateHelper.GetSessionFactory()).LifeStyle.Singleton, Component.For<ISession>().LifeStyle.Transient .UsingFactoryMethod(kernel => kernel.Resolve<ISessionFactory>().OpenSession()) ); //add to the container container.Register( Component.For<IActionInvoker>().ImplementedBy<WindsorActionInvoker>(), Component.For(typeof(IRepository<>)).ImplementedBy(typeof(NHibernateRepository<>)) ); 

It is based on the StructureMap post here http://www.kevinwilliampang.com/2010/04/06/setting-up-asp-net-mvc-with-fluent-nhibernate-and-structuremap/

however, when this is done, a new session is created for each object that it also enters. What am I missing?

(FYI NHibernateHelper, configures for Nhib)

+4
source share
2 answers

ISession must have LifeStyle.PerWebRequest . But you can just use the NHibernate object instead of manually handling these things.

+2
source
 container.AddFacility<FactorySupportFacility>(); container.Register(Component.For<ISessionFactory>() .LifeStyle.Singleton .UsingFactoryMethod(() => new NhibernateConfigurator().CreateSessionFactory())); container.Register(Component.For<ISession>() .LifeStyle.PerWebRequest .UsingFactoryMethod(kernel => kernel.Resolve<ISessionFactory>().OpenSession())); 
+9
source

Source: https://habr.com/ru/post/1307400/


All Articles