32

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?

Shaddix
  • 5,901
  • 8
  • 45
  • 86
  • Note: `Get` is an extension method and you must be `using Ninject;` for it to work. It took me a minute because I have got away with `using Ninject.Modulesl` until now ) – Simon_Weaver Dec 05 '17 at 12:31

1 Answers1

30

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.

Remo Gloor
  • 32,665
  • 4
  • 68
  • 98
  • Picking the constructor is useful if there are two with the same parameter name and different types, in which case, Ninject will throw an exception. eg. `Foo(int x)` and `Foo(string x)` – ashes999 May 29 '13 at 14:29
  • It might also be worth mentioning that you use the Inject method to keep the service context E.g. `ToConstructor(x => new FooA(x.Inject()))` instead of doing `x.Context.Kernel.Get` – LewisM Sep 19 '18 at 09:29