Ways to setup a Ninject singleton

前端 未结 2 608
面向向阳花
面向向阳花 2021-02-07 13:46

I have a class (MyFacade) that I injected parameter(s) with Ninject:

class MyFacade
{
    IDemoInterface demo;

    public MyFacade(IDe         


        
2条回答
  •  无人及你
    2021-02-07 14:11

    When setting up your bindings, you need to bind your dependencies. It is always better to setup your dependencies in your bindings, as opposed to doing a kernel.Get() in a constructor. You are using IOC, so leverage the framework you are using to do the injection for you.

    In your second example binding, what you are missing is binding in your IDemoInterface. Your bindings should look like this:

    //bind the dependency to the implementation.
    kernel.Bind().To();
    //since you bound your dependency, ninject should now have 
    // all the dependencies required to instantiate your `MyFacade` object.
    kernel.Bind().To().InSingletonScope(); 
    

提交回复
热议问题