John Farrell
John Farrell

Reputation: 24754

Resolving a constructor with two arguments of the same interface?

Another developer and had this conversation today:

Me: Dependency Injection is cool, lol.

Dennis: What happens when I need an instance of the DoStuff class and the only constructor I have is DoStuff( ISomeInterface interface1, ISomeInterface interface2 ) where the concrete types are completely different?

Me: ...

We use Unity as our preferred container. How would I register that when I need to resolve ISomeInterface that the concrete type can be two different types?

Upvotes: 1

Views: 259

Answers (1)

charoco
charoco

Reputation: 133

Take a look at the ParameterOverride class. It allows you to specify parameters by name:

   container.Resolve<IDoStuff>(new ParameterOverrides<DoStuff> { { "interface1", new SomeInterfaceImpl() }, { "interface2", AnotherSomeInterfaceImpl() } });

Upvotes: 3

Related Questions