Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unity MVC3 Controller Constructor issue [duplicate]

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?

like image 777
scarlin Avatar asked Dec 04 '25 19:12

scarlin


1 Answers

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")
))
like image 95
Artem Vyshniakov Avatar answered Dec 07 '25 14:12

Artem Vyshniakov