What's the difference between .ToConstructor and .ToMethod in Ninject 3?

前端 未结 1 489
北恋
北恋 2020-12-30 20:10

In Ninject3 there\'s a new .ToConstructor feature.

As described, it helps to strongly-type constructor arguments like:

Bind().ToCon         


        
相关标签:
1条回答
  • 2020-12-30 20:55

    The first case behaves like To<MyService>() except that you explicitly select the constructor. This means the context is passed through MyService and you can use conditions for IFoo and IBar or one of their dpependencies where in the second case you get a new context for IFoo and IBar and you will not know that they are injected into MyService.

    e.g.

    Bind<IFoo>().To<FooA>().WhenInjectedInto<MyService>();
    Bind<IFoo>().To<FooB>().WhenInjectedInto<MyOtherService>();
    

    will not work in the second case.

    0 讨论(0)
提交回复
热议问题