Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ninject passing in constructor values

With Ninject, how do you configure the kernel so I can define what constructor values are passing into the instantiation of an object?

I have the following configured in a module:

Bind<IService1>()
    .To<Service1Impl>()
    .InSingletonScope()
    .Named("LIVE");
Bind<IService2>()
    .To<Service2Impl>()
    .InSingletonScope()
    .Named("LIVE")
    .WithConstructorArgument(
        "service1", 
        Kernel.Get<IService1>("LIVE"));

Service2Impl takes a constructor parameter of IService1 but I want this to come from the container. I also want to have named bindings as my code will be targeting different versions at runtime.

This seems to work but is it the right way to achieve what I want to do? Should I be achieving without the use of named bindings and wiring different configuration modules into the kernel?

EDIT

I have used the ToMethod() method now to specify a delegate to call on request of a specific type. This seems a bit nicer as I'll get compile time warnings if the constructor configuration is wrong rather than having to know the name of the parameter I am passing first.

Thanks

like image 735
Tim Peel Avatar asked May 19 '11 22:05

Tim Peel


3 Answers

I would recommend the WithConstructorParameter overload that takes a lambda like so:

Bind<IService2>()
    .To<Service2Impl>()
    .InSingletonScope()
    .Named("LIVE")
    .WithConstructorArgument(
        "service1", 
        ctx => ctx.Kernel.Get<IService1>("LIVE"));

This will ensure that that the resolution of IServive1 happens at the time of activation of Service2Impl and not at startup when the container is created. Whilst in your case it doesn't really matter as Service1Impl is singleton, there could be side effects on doing it in the way you originally wrote it:

  • The binding for dependency that is injected by WithConstructorArgument has to already exist. This implies that all bindings have to done in a particular order. This creates can get tricky when there are multiple modules involved.

  • Scoping issues can arise when custom scope is used. Ninject 2.0 introduced cache and collect scope management, binding to a constant is very likely to throw that into disarray.

like image 170
Igor Zevaka Avatar answered Oct 22 '22 16:10

Igor Zevaka


I used ToMethod in the end, which allowed me to construct the required instance with constructors in order to maintain compile time errors.

For example:

.ToMethod(Func<IContext, T> method)

Bind<IWeapon>().ToMethod(context => new Sword());
like image 37
Tim Peel Avatar answered Oct 22 '22 17:10

Tim Peel


It seems you're looking at this the wrong way. Ninject will inject service 1 automatically into service 2 if it has it as constructor argument. There is not need for WithConstructorArgument in this case.

If there are multiple IService1 you should go for conditions. E.g. WhenParentNamed(...)

like image 6
Remo Gloor Avatar answered Oct 22 '22 17:10

Remo Gloor