如何使用Ninject更改要注入的Concrete类。到目前为止,我为每个具体类都有一个接口。我现在有一个单独的接口的两个具体类,其中一个需要根据我正在注入的属性/控制器被选择。
class IService {}
class ConcreteServiceA : IService {}
class ConcreteServiceB : IService {}
// Where the binding happens-
private static void RegisterServices(IKernel kernel)
{
kernel.Bind<IService>().To<ConcreteServiceA>();
kernel.Bind<IService>().To<ConcreteServiceB>();
}
class ControllerA {
[Inject]
// Needs to be ConcreteServiceA
public IService _service { get; set; }
}
class ControllerB {
[Inject]
// Needs to be ConcreteServiceB
public IService _service { get; set; }
}
答案 0 :(得分:0)
您可以在NInject Method WhenInjectedInto
之后尝试此操作Bind<IService>().To<ConcreteServiceA>().WhenInjectedInto(typeof(ControllerA));
Bind<IService>().To<ConcreteServiceB>().WhenInjectedInto(typeof(ControllerB));