我試圖讓溫莎給我一個實例的Isession爲每個請求,應注入到所有存儲庫溫莎+ NHibernate的+的Isession + MVC
這裏是我的容器設置
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<>))
);
它基於這裏的StructureMap http://www.kevinwilliampang.com/2010/04/06/setting-up-asp-net-mvc-with-fluent-nhibernate-and-structuremap/
但是,當它運行時,會爲每個注入的對象創建一個新的Session。我錯過了什麼?
(FYI的NHibernateHelper,建立了Nhib的配置)
謝謝,我會研究PerWebRequest。我對Facility不太感興趣,因爲我不想要SessionManager的依賴。 – dbones 2010-04-20 09:13:42
您可以忽略ISessionManager並注入ISessionFactory。甚至創建一個額外的工廠註冊並注入ISession。 – 2010-04-20 11:36:53