NInject: Where do you keep your reference to the Kernel?

后端 未结 3 709
终归单人心
终归单人心 2021-01-30 06:43

I\'m using NInject on a new web application and there are two things that are unclear to me:

  1. Don\'t I need to keep a reference to the Kernel around (Session/App

3条回答
  •  深忆病人
    2021-01-30 07:14

    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.)

提交回复
热议问题