Possible Duplicate:
Unity IOC container and how to resolve different instances of the same interface
I have a controller constructor that has two parameters that implement the same interface shown below. I have tried to register these types in Unity also shown below but I have run into a problem.
Controller Constructor
public ControlController(IAdapter daveAdapter, IAdapter bobAdapter)
{
DaveAdapter = daveAdapter;
BobAdapter = bobAdapter;
}
Unity registration
container
.RegisterType<IAdapter, DaveAdapter>()
.RegisterType<IAdapter, BobAdapter>()
When the controller is constructed both adapters are resolved as DaveAdapter's instead of one Dave and one Bob. How can I tell unity to differentiate between the two adapters so that the controller has an adapter of each type?
You can use InjectionConstructor and ResolvedParameter objects in registering controller:
.RegisterType<IAdapter, DaveAdapter>()
.RegisterType<IAdapter, BobAdapter>("Bob")
.RegisterType<ControlController, ControlController>(
new InjectionConstructor(
new ResolvedParameter<IAdapter>(),
new ResolvedParameter<IAdapter>("Bob")
))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With