Ninject passing in constructor values

2019-02-08 12:02发布

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

5条回答
别忘想泡老子
2楼-- · 2019-02-08 12:03

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

查看更多
乱世女痞
3楼-- · 2019-02-08 12:11

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楼-- · 2019-02-08 12:15

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.

查看更多
forever°为你锁心
5楼-- · 2019-02-08 12:16

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

Bind<IService2>().ToConstant(new Service2Impl(argument)))
                 .Named("LIVE");
查看更多
闹够了就滚
6楼-- · 2019-02-08 12:23

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());
查看更多
登录 后发表回答