Let’s say I have the following classes and interfaces in my MVC 2 project:
Repositories:
IRepository1, IRepository2, IRepository3
public class ConcreteRepository1 : IRepository1
{
public ConcreteRepository1()
{
...
}
...
}
public class ConcreteRepository2 : IRepository2
{
public ConcreteRepository2()
{
...
}
...
}
public class ConcreteRepository3 : IRepository3
{
public ConcreteRepository3()
{
...
}
...
}
Service classes:
public class Service1
{
private IRepository1 repo1;
private IRepository2 repo2;
public Service1(IRepository1 repo1, IRepository2 repo2)
{
this.repo1 = repo1;
this.repo2 = repo2;
}
...
}
Controllers:
public class Controller1 : Controller
{
private Service1 srv1;
private Service2 srv2;
public Controller1(Service1 srv1, Service2 srv2)
{
this.srv1 = srv1;
this.srv2 = srv2;
}
...
}
I have custom ControllerFactory and I know how to tie concrete repositories to interfaces:
IUnityContainer container = new UnityContainer();
container.RegisterType<IRepository1, ConcreteRepository1>(new TransientLifetimeManager(), new InjectionConstructor());
container.RegisterType<IRepository2, ConcreteRepository2>(new TransientLifetimeManager(), new InjectionConstructor());
container.RegisterType<IRepository3, ConcreteRepository3>(new TransientLifetimeManager(), new InjectionConstructor());
The question is how should I register services’ instances and controllers’ types inside my custom ControllerFactory to make unity container resolve the whole hierarchy Controller->Service->Repository and avoid calling Resolve inside controllers or services?
Thanks.
If you have already registered
IRepository1-3, so you can getService1instance simply callingCalling
container.Resolve<Controller1>()will automatically resolve dependencies and will create instance of typeController1.Sample:
Resolving:
EDIT:
Output:
So, when we haven’t registered
Service(in other words, it has default resolving behavior), Unity constructsServiceusing greediest constructor by default. When you specifynew InjectionConstructor(), this tells Unity to use parameterless constructor. The same behavior can be received if you mark constructor (in this casepublic Service()) withInjectionConstructorAttribute.