Wilbert
Wilbert

Reputation: 7399

Ninject v2+ injection dependent on parametername

I know that with Ninject 1, it was possible to give specific instances depending on the parameter name.

E.g public SomeClass(ISomething left, ISomething right) {}

would resolve ISomething left to the default ISomething, but ISomething right to a different binding.

How can I do this with Ninject 2+?

Note: I do not want to use [Named("XZ")], because that would force me to make the main dll of my application a dependency of Ninject. Right now I have a separate DLL CompositionRoot that depends on Ninject, and nothing else: my main dll can be used without Ninject. I would like to keep it that way.

Edit: I updated the example to use left/right instead of simple/complex after the first answer. I do not want to hardcode this dependency in my inheritance structure.

Upvotes: 0

Views: 107

Answers (2)

Wilbert
Wilbert

Reputation: 7399

I figured it out:

Bind<ISomething >().To<LeftSomething>().When(a => a.Target.Name == "left");

Upvotes: 3

Daniel A. White
Daniel A. White

Reputation: 190943

Why don't you create derived interfaces ISimpleSomething and IComplexSomething?

That way you could expose additional functionality for each.

interface ISomething { ... }
interface IComplexSomething : ISomething { ... }
interface ISimpleSomething : ISomething { ... }

Upvotes: 2

Related Questions