0

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.

4

2 回答 2

3

我想到了:

Bind<ISomething >().To<LeftSomething>().When(a => a.Target.Name == "left");
于 2013-09-16T12:47:54.653 回答
2

为什么不创建派生接口ISimpleSomethingIComplexSomething

这样,您可以为每个公开附加功能。

interface ISomething { ... }
interface IComplexSomething : ISomething { ... }
interface ISimpleSomething : ISomething { ... }
于 2013-09-12T12:07:46.217 回答