NHibernate的,依赖注入。关闭ISession的正确 [英] NHibernate, dependency injection. Close ISession properly
问题描述
我使用Ninject,NHibernate的,ASP.NET MVC3和存储库的模式。模块中Ninject结合如以下
I am using Ninject, NHibernate, ASP.NET MVC3 and repository pattern. The module binding in Ninject is as following.
Bind<ISessionFactory>().ToProvider(new SessionFactoryProvider()).InSingletonScope();
Bind<ISession>().ToMethod(context => context.Kernel.Get<ISessionFactory>().OpenSession()).InRequestScope();
问题是应该采取库一个ISession或ISessionFactory。如果它需要一个ISessionFactory然后在仓库中,我可以在必要的时候打开一个会话使用后关闭。如果需要一个ISession,库直接使用它。但是,如果会话正确关闭我想知道。
The question is should the repository take an ISession or ISessionFactory. If it takes an ISessionFactory then in the repository I can open a session when necessary and close it after use. If it takes an ISession, the repository uses it directly. But I am wondering if the session is closed properly.
推荐答案
我一般打开一个请求开始一个新的会话和交易,并提交/关闭它的结束。
I usually open a new session and transaction on the beginning of a request and commit/close it on the end.
看这个<一个href=\"http://nhforge.org/blogs/nhibernate/archive/2011/03/03/effective-nhibernate-session-management-for-web-apps.aspx\"相对=nofollow>张贴在nhforge 。这篇文章超出了你的需求,我认为这将帮助你很多。就拿在他写的自定义的HttpModule更好看。这只是一个例子,你可以在谷歌搜索找到许多类似的实现。
Look at this post on nhforge. This post goes beyond your needs, I think it will help you a lot. Take a better look at the custom HttpModule he wrote. That's just an example, you can search at Google and find lots of similar implementations.
这篇关于NHibernate的,依赖注入。关闭ISession的正确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!