我可以指定Ninject的范围,只有这个具体的情况?范围、具体、情况、Ninject

2023-09-03 05:07:55 作者:清风兑酒

前面我问this问题。这个问题的答案导致到另一个问题就像你在你面前看见。

Earlier I asked this question. The answer to which resulted into another question like thou seeth before thee.

我的问题是,我有一个自定义的的MembershipProvider 使用 AccountRepository 使用 ObjectContext的。因为的MembershipProvider 辛格尔顿的MVC(据我所知),在 AccountRepository 的ObjectContext 应注射一次,并在那里呆了的的MembershipProvider 的余生时间。

My problem is, that I have a custom MembershipProvider using an AccountRepository using an ObjectContext. Because the MembershipProvider is a Singleton in MVC (as I understand), the AccountRepository and its ObjectContext should be injected once and stay there for the remainder of the MembershipProvider's life time.

不过,在我的控制器我还使用存储库与对象上下文。在这些控制器我需要的对象上下文要与一个要求的储存库之间共享。我有以下的绑定:

However, in my controllers I also use repositories with object contexts. In these controllers I need the object context to be shared between the repositories with a request. I have the following binding:

Bind<IMyContext>().To<MyObjectContext>().InRequestScope();
// put bindings here
Bind<IAccountRepository>().To<EFAccountRepository>

的Application_Start()

kernel.Inject(Membership.Provider);

问题是,Ninject显然调用Dispose对象上下文上时,它认为要求做(我觉得30秒后)。

The problem is that Ninject apparently calls dispose on the object context when it thinks the request is done (I think after 30 seconds).

我注意到,当您设置绑定,您可以指定注入时。现在的问题是,我需要注入注入到时的时候。即注射时的对象上下文的到帐户控制注射时帐户控制到的成员资格提供程序。而我似乎没有有...

I noticed that when you set your bindings you can specify "when injecting into". The problem is, that I need "when injecting into when injecting into". I.e. when injecting the object context into the account controller when injecting the account controller into the membership provider. And I don't seem to have that...

在MVC不要挂 MyMembershipProvider 。只是考绩它(后面接口)的实例给需要它就像我做的库控制器。然后Ninject将实例每次请求的提供者。我不喜欢它,因为我敢肯定,MVC有一个原因实例成员资格提供作为一个单身。 找到了再次发生的每个请求和呼叫 kernel.Inject 中的每个事件的事件。重新初始化提供的每个请求是几乎等同于重新实例化,除脏。 请为会员提供一个单独的帐户存储库,我可以以不同的方式绑定。它似乎没有权利改变我的对象,因为Ninject的模式。 Don't hang MyMembershipProvider in MVC. Just pas an instance of it (behind and interface) to the controllers that need it like I do with repositories. Then Ninject will instantiate the provider per request. I don't like it because I'm sure MVC has a reason for instantiating the membership provider as a singleton. Find an event that occurs every request and call kernel.Inject again in each event. Re-initializing the provider every request is nearly equivalent to re-instantiating, except dirtier. Make a separate account repository for the membership provider to which I can bind in a different way. It doesn't seem right to change my object model because of Ninject.

恕我直言,第一解决办法是最好的。不过,我更愿意找到一种方法来设置Ninject绑定在我想要的方式。

Conclusion

IMHO the first work-around is the best. However, I much rather find a way to set Ninject to bind in the way I want.

我应该怎么办?

推荐答案

.InRequestScope()位是造成你的一些对象来进行处理(见的缓存和收集)。

The .InRequestScope() bit is causing some of your objects to be Disposed (see Cache and Collect).

您需要做这个(应要求作用域)不会发生在你对你的供应商的依赖配置。

You need to make this (scoping by request) not happen in your provisioning of your provider's dependencies.

要实现这一目标的方法之一是有两个绑定 - 一个用于请求处理分支,一个用于全球范围内,即,添加 ...它使用在 context.ParentContext ...链条来控制它是否 InRequestScope()或不

One way to achieve that is by having two bindings - one for the request processing branch and one for the global context, i.e., adding a When... which uses the context.ParentContext... chain to control whether it's InRequestScope() or not

不幸的是我没有时间给现在一个完整的答案和相关文档外出与code为present同步。

Sadly I don't have time to give a full answer now and the relevant docs are out of sync with the code at present.

 
精彩推荐
图片推荐