将Ninject调用Dispose并关闭NHibernate的ISession的? [英] Will Ninject call dispose and close the NHibernate Isession?

查看:179
本文介绍了将Ninject调用Dispose并关闭NHibernate的ISession的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用ASP.NET MVC 3 Ninject和NHibernate。

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

当DI的思考,我认为谁得到的资源也一确保将其关闭(在这种情况下Ninject应该负责)

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)

但我不知道使用InRequestScope时Ninject是如何工作的。

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

我的code是:

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

我打开一个会话,并把它放在我InRequestScope,而将采取Ninject我收的ISession的时候才出来请求范围的?

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?

推荐答案

如果我理解code正确答案是肯定的。一个接Ninject使用的Activati​​onStrategies的是DisposableStrategy,其停用方法,在任何实现IDisposable调用Dispose。如果您使用的Ninject.Web.MVC扩展时,OnePerRequestModule将自动清除绑定缓存。这将呼吁所有Activati​​onStrategies包括DisposableStrategy停用方法。

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.

自的ISession实现IDisposable,它会被设置。 ISession的,SessionImpl,默认实现逢处置会话。

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

如果你不使用的Ninject.Web.MVC扩展可以缓存最终会被清除,但EndRequest可能不会发生的权利。

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

这篇关于将Ninject调用Dispose并关闭NHibernate的ISession的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆