Ninject 2.0 - binding to a object that uses the same interface more than once?

binding, c#, ioc-container, ninject

Solution

In addition to the use of "Only" method, the article suggests another solution by specifying different attributes for the injected objects.

Example:

public class ObjectOneAttribute : Attribute
{

}  
public class ObjectTwoAttribute : Attribute
{

}

Then

public Something([ObjectOneAttribute] IInterface concreteObjectOne, [ObjectTwoAttribute] IInterface concreteObjectTwo) 
    {
        this.concreteObjectOne = concreteObjectOne;
        this.concreteObjectTwo = concreteObjectTwo;
    }

And when you want to bind the interface to the correct concrete object, use the "WhereTargetHas" method:

Bind<IInterface>().To<YourConcreteTypeOne>().WhereTargetHas<ObjectOneAttribute>();
Bind<IInterface>().To<YourConcreteTypeTwo>().WhereTargetHas<ObjectTwoAttribute>();

Update: Solution without using attributes: Use the method "When":

Bind<IInterface>().To<YourConcreteTypeOne>().When(r => r.Target.Name == "concreteObjectOne");  
Bind<IInterface>().To<YourConcreteTypeTwo>().When(r => r.Target.Name == "concreteObjectTwo")

;

Problem

Consider the following: ``` public Something(IInterface concreteObjectOne, IInterface concreteObjectTwo) { this.concreteObjectOne = concreteObjectOne; this.concreteObjectTwo = concreteObjectTwo; } ``` How do I set set this type of binding up with Ninject? I'd try Googling the term, but as I'm not sure what this is called I can't, nor can I find anything on the Wiki about this. Edit: I believe this is called Convention Based Binding, as described here. However this documentation is for version 1.0 and 2.0 does not have the `Only` method. I'd like this to be achieved without attributes - using the convention of names or something similiar.

Original source