Pregunta

I'm using Ninject 2.0 to handle DI in one of my apps and I've come across something that's confusing me. Having zero documentation doesn't help too much either to be honest.

Say I have a constructor with the signature -

ctor(IServiceFactory factory1, IServiceFactory factory2)
{
    this.factory1 = factory1;
    this.factory2 = factory2;
}

Although these two services implement the same interface, they are quite different implementations and are used at different times so I don't want to inject an IEnumerable<IServiceFactory>.

My question is, when I'm binding the instances, how do I tell Ninject what to inject for each?

Thanks in advance.

Update

For the sake of anyone wanting to see the code would end up after reading Remo's links,...Here it is in brief. (I never realised C# had parameter attributes!)

//abstract factory
public interface IServiceFactory
{
    Service Create();
}

//concrete factories
public class Service1Factory : IServiceFactory
{
    public IService Create()
    {
        return new Service1();
    }
}

public class Service2Factory : IServiceFactory
{
    public IService Create()
    {
        return new Service2();
    }
}

//Binding Module (in composition root)
public class ServiceFactoryModule : NinjectModule
{
    public override void Load()
    {
        Bind<IServiceFactory>()
            .To<Service1Factory>()
            .Named("Service1");

        Bind<IServiceFactory>()
            .To<Service2Factory>()
            .Named("Service2");
    }
}

//consumer of bindings
public class Consumer(
    [Named("Service1")] service1Factory,
    [Named("Service2")] service2Factory)
{
}
¿Fue útil?

Solución

First of all you have to ask yourself if using the same interface is correct if the implementations need to do a completely different thing. Normally, the interface is the contract between the consumer and the implementation. So if the consumer expects different things then you might consider to define different interfaces.

If you decide to stay with the same interface than you have to use conditional bindings. See the documentation about how this is done:

https://github.com/ninject/ninject/wiki/Contextual-Binding

https://github.com/ninject/ninject/wiki/Conventions-Based-Binding

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top