Ninject passing in constructor values

对着背影说爱祢 提交于 2019-11-29 03:40:38

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.

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

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

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

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

Bind<IService2>().ToConstant(new Service2Impl(argument)))
                 .Named("LIVE");
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!