Ninject passing in constructor values

妖精的绣舞 提交于 2019-12-29 05:55:29

问题


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


回答1:


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.




回答2:


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());



回答3:


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(...)




回答4:


Maybe the Providers can help you. Bind IService2 To a Provider. and in the Create method of Provider, use Kernel.Get("LIVE") to create the Service2Impl instance.

see the following link to know how to use Provider https://github.com/ninject/ninject/wiki/Providers%2C-Factory-Methods-and-the-Activation-Context




回答5:


I think ToConstant() is cleaner, the InSingletonScope is implicit:

Bind<IService2>().ToConstant(new Service2Impl(argument)))
                 .Named("LIVE");


来源:https://stackoverflow.com/questions/6065464/ninject-passing-in-constructor-values

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!