In Ninject3 there’s a new .ToConstructor feature.
As described, it helps to strongly-type constructor arguments like:
Bind<IMyService>().ToConstructor(
ctorArg => new MyService(ctorArg.Inject<IFoo>(), ctorArg.Inject<IBar>()));
What’s actually the difference between using .ToConstructor and .ToMethod in an almost the same way:
Bind<IMyService>().ToMethod(
x => new MyService(x.Kernel.Get<IFoo>(), x.Kernel.Get<IBar>()));
Is it just a syntax sugar to avoid using Kernel.Get<>() or is there something more that I’m missing?
The first case behaves like
To<MyService>()except that you explicitly select the constructor. This means the context is passed throughMyServiceand you can use conditions forIFooandIBaror one of their dpependencies where in the second case you get a new context forIFooandIBarand you will not know that they are injected intoMyService.e.g.
will not work in the second case.