Will Ninject call dispose and close the NHibernate Isession?

╄→尐↘猪︶ㄣ 提交于 2019-12-04 18:54:34

问题


I'm using ASP.NET MVC 3 with Ninject and NHibernate.

When thinking of DI, i think the one who get the resource also makes sure to close it(In this case Ninject should be responsible)

But I'm not sure how Ninject works when using InRequestScope.

My code is:

Bind<ISession>().ToMethod(context => context.Kernel.Get<ISessionFactory>().OpenSession()).InRequestScope();

I open a session and put it in I InRequestScope, but will Ninject take of closing my ISession when it is out of request scope?


回答1:


If I understand the code correctly the answer is yes. One of the ActivationStrategies used by Ninject is the DisposableStrategy, whose Deactivate method, calls Dispose on anything that implements IDisposable. If you're using the Ninject.Web.MVC extensions, the OnePerRequestModule will automatically clear the binding cache. This will call the Deactivate method on all the ActivationStrategies including the DisposableStrategy.

Since ISession implements IDisposable, it will be disposed. The default implementation of ISession, SessionImpl, closes the Session on Dispose.

If you're not using the Ninject.Web.MVC extensions the Cache will eventually be cleared, but may not happen right at EndRequest.



来源:https://stackoverflow.com/questions/5243228/will-ninject-call-dispose-and-close-the-nhibernate-isession

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!