1

My question resembles this question. However there is an important difference. I am using Unity Container. The problem is that I need to use two instance of an interface in the same constructor. These instances may be from the same implementation as well as different ones. Simplified version of the constructor is below.

public SomeService(IMyInterface instance1, IMyInterface instance2) : ISomeService

In the question I mentioned before, different implementations were used in different classes. However in my case, I need separation in one class.

Is this possible with Unity? If not, is there a recent container which has this capability?

Community
  • 1
  • 1
Nuri Tasdemir
  • 9,720
  • 3
  • 42
  • 67

1 Answers1

1

You can use named registrations and InjectionConstructor like this:

container.RegisterType<IMyInterface, Impl1>("Impl1");
container.RegisterType<IMyInterface, Impl2>("Impl2");
container.RegisterType<ISomeService, SomeService>(
    new InjectionConstructor(
        new ResolvedParameter<IMyInterface>("Impl1"),
        new ResolvedParameter<IMyInterface>("Impl2")));

var service = container.Resolve<ISomeService>();

IMO, it is better not to use a DI container and instead use Pure DI when you have "complex" object graphs like what you have here. See my article here for more details.

Yacoub Massad
  • 27,509
  • 2
  • 36
  • 62