Ninject:是否有可能在TransientScope在SingletonScope父对象和子? [英] Ninject: Is it possible to have parent object in SingletonScope and child in TransientScope?

查看:217
本文介绍了Ninject:是否有可能在TransientScope在SingletonScope父对象和子?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在货架我和关闭这个现在几个星期...大脑我currenlty有是这样的:

I have been racking my brain on and off with this for a few weeks now... What I currenlty have is this:


  • 一束 *服务

  • 所有这些都依赖于不同的 *,它通过EF访问数据库存储库

  • 要允许单元测试的DbContext 的衍生物被注入到存储库。 (所以我不能使用使用来处置上下文)

  • A bunch of *Service classes
  • All of these depend on different *Repository classes that access database via EF
  • To allow Unit Testing a derivate of DbContext is injected into repositories. (so I can not use using to dispose of the contexts)

要正确处理被注射我可以运行在我的依赖关系树InRequestScope()或一个简单的自定义范围EF上下文 - InScope (C =>新的对象())。在顶层,并在 InParentScope()上的所有其他级别

To correctly dispose of the EF contexts that are injected I could run my dependency tree in InRequestScope() or in a simple custom scope - InScope(c => new object()) on the top level and in InParentScope() on all other levels.

这两种方法都将产生和每个请求中处理大量的对象。此外,我们是在谈论一个单页的应用程序,以便查询的95%(50个左右),将在2个请求被执行如此 InRequestScope()似乎不是一个好主意。另外, *服务类容不下状态,因此可能是 InSingletonScope()键,将最大限度地减少创建对象的数量。

Both of these approaches would create and dispose a lot of objects during each request. In addition we are talking about a single page application so that 95% of the queries (50 or so) will be executed during 2 requests so InRequestScope() seems not to be a good idea. Also the *Service classes hold no state and thus could be InSingletonScope() and would minimize the amount of object creation.

问题

时有可能有父 *服务 *在类InSingletonScope( )并以某种方式在一个范围内,将在每次访问时返回一个新的实例,并会履行 IDisposable的注入EF 的DbContext 使用NInject?

Is it possible to have parent *Service and *Repository classes in InSingletonScope() and somehow inject EF DbContext in a scope that will return a new instance each time it is accessed and will honor IDisposable using NInject?

我知道当对象被创建的依赖注入,但可能这仍然以某种方式管理?

I know that dependencies are injected while objects are created but could this still be somehow managed?

推荐答案

没有,这是不可能的。如果你仔细想想,你应该明白为什么。

No, it's not possible. If you think about it, you should understand why.

一个单独的对象将存在于应用程序的生命周期。一个InRequestScope对象住要请求的寿命。由于您的单库将永远活着,并且将举办一个关于你的DbContext(因为它是一个依赖),这意味着的DbContext不能被垃圾收集没有你的资料库有一些机制来释放它。

A singleton object will exist for the lifetime of the application. An InRequestScope object lives for the lifetime of a request. Since your singleton repository will live forever, and it will hold a reference to your DbContext (because it's a dependency), that means the dbcontext cannot be garbage collected without your repository having some mechanism to release it.

即使你没有提供这样的机制,你必须有另一种机制,以重新获得对下一个请求一个新的实例,因为一个新的单例存储库将不会创建(因此它的构造函数不会被调用,因此,它的依赖不会被解决,因此它不会对新的DbContext能够知道)。

Even if you did supply such a mechanism, you'd have to have another mechanism to re-acquire a new instance on the next request, because a new singleton repository will not be created (and thus it's constructor will not be called, and thus it's dependencies will not be resolved, and thus it will not be able know about the new dbcontext).

所以,实际上,作出InRequestScope对象一个singleton对象的依赖将有效地使InRequestScope对象单身,否则对象将被从单下可设置出来,这可能是坏..

So, in effect, making an InRequestScope object a dependency of a singleton object would effectively make the InRequestScope object a singleton, or else the object will be disposed out from under the singleton and that could be bad..

另外,我求的事实,你的资料库 DO 的有状态与你不同。该州是的DbContext本身。由于单身是一种应用广泛的静态实例,您可以使用您的应用程序,这将意味着它也将给予同样的DbContext,这是一个巨大给出相同的情况下对所有用户没有没有,因为你的用户会踩遍每个人的的DbContext状态。

Also, I beg to differ with you on the fact that your repositories DO have state. The state is the DbContext itself. Since a Singleton is an application wide static instance, you give the same instance to all users using your app, which would mean it would also give the same DbContext, which is a huge no-no, since your users would stomp all over each others DbContext state.

您服务也是同样,有状态的,因为他们有仓库,这是我刚才指出有DbContexts,这是有状态的。

Your services are also, likewise, stateful because they have repositories, which as I've just pointed out have DbContexts, which are stateful.

您想要做的只是让你的服务,存储库和DbContexts所有InRequestScope什么。

What you want to do is simply make your Services, repositories, and DbContexts all InRequestScope.

我不明白为什么这种做法将每一个请求中创建大量的对象。整点是,它仅创建每个请求的每个对象类型的一个实例。

I fail to understand why this approach would "create lots of objects during each request". The whole point is that it only creates one instance of each object type per request.

这篇关于Ninject:是否有可能在TransientScope在SingletonScope父对象和子?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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