NInject:你在哪里让你的参考内核? [英] NInject: Where do you keep your reference to the Kernel?
问题描述
我在一个新的Web应用程序中使用NInject,有两件事情是我不清楚:
-
唐'T我需要保持对内核的引用各地(会话/应用程序变量),以确保GC不会收集我所有的实例?例如,如果我指定。采用(),然后内核对象被收集的,不是我的所有的单身收集呢?
-
如果我确实需要保持一个内核对象的引用身边,我怎么允许传递给WithArguments()来改变的参数或者是说不可能的。
这是真的,你不希望绕过内核。通常情况下,在一个Web应用程序,我存储在HttpApplication的静态属性内核。如果您需要内核的引用,你可以公开的依赖(通过构造函数参数或属性),该类型的iKernel,并Ninject会给你的激活型内核的引用。
如果您上的结合使用WithArguments(),它们将被用于所有的激活。如果使用i参数,它们将只用于该活化。 (不过,如果你激活的服务有一个可重复使用的行为像单身,也不会被重新激活,即使你通过不同的i参数)。
I'm using NInject on a new web application and there are two things that are unclear to me:
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.
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.
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.)
这篇关于NInject:你在哪里让你的参考内核?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!