0

How do I change the Concrete class to inject using Ninject. So far I have a single interface for each concrete class. I now have two concrete classes for a single interface where one needs to be chosen over the other depending on the attribute/controller I'm injecting.

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; }
}
micah
  • 7,596
  • 10
  • 49
  • 90
  • has many duplicates already: http://stackoverflow.com/questions/3313940/ninject-to-bind-on-different-controllers http://stackoverflow.com/questions/26854101/ninject-multiple-classes-using-single-interface-more-than-one-matching-bindin/26854310#26854310 http://stackoverflow.com/questions/21287648/bind-multiple-implementations-to-the-same-interface-with-ninject/21308783#21308783 – BatteryBackupUnit Apr 22 '17 at 14:23

1 Answers1

0

You can try this following NInject Method WhenInjectedInto

Bind<IService>().To<ConcreteServiceA>().WhenInjectedInto(typeof(ControllerA));
Bind<IService>().To<ConcreteServiceB>().WhenInjectedInto(typeof(ControllerB));