Interface with multiple implementations in ninject

不问归期 提交于 2019-12-11 03:34:23

问题


I've got an interface that has two different implementations.

public interface IProducer
{
}

public class Producer : IProducer
{
}

public class FaultProducer : IProducer
{
}

I have two different classes that both take an IProducer as a dependency.

public class ConsumerChannel
{
    public ConsumerChannel(IProducer producer)
    {
    }
}

public class TradePublisher
{
    public TradePublisher(IProducer producer)
    {
    }
}

TradePublisher needs a Producer and ConsumerChannel needs a FaultProducer. I can only bind IProducer to one implementation. In Windsor I could do this with named bindings and Dependency.OnComponent but I can't find similar abilities in Ninject. Is there a way to inject specific dependencies in Ninject?


回答1:


Use named bindings for Ninject:

Bind<IProducer>().To<FaultProducer>().Named("FaultProducer");

public TradePublisher([Named("FaultProducer")] IProducer producer)
    //...
}


来源:https://stackoverflow.com/questions/19119591/interface-with-multiple-implementations-in-ninject

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