C# NInject:你在哪里保存对内核的引用?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/590900/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
NInject: Where do you keep your reference to the Kernel?
提问by JC Grubbs
I'm using NInject on a new web application and there are two things that are unclear to me:
我在一个新的 Web 应用程序上使用 NInject,有两件事我不清楚:
Don't I need to keep a reference to the Kernel around (Session/App variable) to insure that GC doesn't collect all my instances? For example, if I specify .Using() and then the Kernel object gets collected, aren't all my "singletons" collected as well?
If I do need keep a reference to a Kernel object around, how do I allow the arguments passed in to WithArguments() to change or is that not possible.
我不需要保留对内核的引用(会话/应用程序变量)以确保 GC 不会收集我的所有实例吗?例如,如果我指定 .Using() 然后收集 Kernel 对象,那么我所有的“单例”不也收集了吗?
如果我确实需要保留对 Kernel 对象的引用,我如何允许传递给 WithArguments() 的参数发生变化,或者这是不可能的。
回答by Mauricio Scheffer
This is a common pitfall when starting to use a IoC container. See this related question.
这是开始使用 IoC 容器时的常见陷阱。请参阅此相关问题。
In a nutshell:
简而言之:
- It's bad practice to pass your container around (been there, done that, and it really hurts)
- If you really need to invocate directly the container, first consider abstracting to an injected factory, then as a last resource consider using a static gateway to the container
- 传递你的容器是不好的做法(去过那里,做过,真的很痛)
- 如果你真的需要直接调用容器,首先考虑抽象为注入的工厂,然后作为最后的资源考虑使用到容器的静态网关
回答by Nate Kohari
It's true that you don't want to pass around the kernel. Typically, in a web app, I store the kernel in a static property in the HttpApplication. If you need a reference to the kernel, you can just expose a dependency (via constructor argument or property) that is of the type IKernel, and Ninject will give you a reference to the kernel that activated the type.
确实,您不想传递内核。通常,在 Web 应用程序中,我将内核存储在 HttpApplication 的静态属性中。如果您需要对内核的引用,您可以只公开一个 IKernel 类型的依赖项(通过构造函数参数或属性),Ninject 将为您提供对激活该类型的内核的引用。
If you use WithArguments() on a binding, they will be used for all activations. If you use IParameters, they will only be used for that activation. (However, if the service you're activating has a re-usable behavior like Singleton, it won't be re-activated even if you pass different IParameters.)
如果在绑定上使用 WithArguments(),它们将用于所有激活。如果您使用 IParameters,它们将仅用于该激活。(但是,如果您正在激活的服务具有像 Singleton 这样的可重用行为,即使您传递不同的 IParameters,它也不会被重新激活。)
回答by satish
Mark Seeman -- author of Manning Dependency InjectionSuggust to Use Hollywood principleDon't call us(IOC framework) instead We will call you ... .. The IOC container should be placed in the Application's Composition root.. and it needs to instantiated as requested.. like wat nate mentioned
Mark Seeman —— Manning Dependency Injection 的作者 建议使用好莱坞原则不要打电话给我们(IOC 框架) 我们会打电话给你......根据要求实例化..就像提到的wat nate
.. For the Web Application the Composition root is Global.asax file where the u can use the override the startup events and There u can bind your Ninject to resolve the component
.. 对于 Web 应用程序,组合根是 Global.asax 文件,您可以在其中使用覆盖启动事件,然后您可以绑定您的 Ninject 来解析组件