Reputation: 263
I am working on enterprise application using MVC, Nhibernate and IOC container.
I three tiers:
DAL - Nhibernate Data Repositories
public class CustomerRepository : Repository<int>,ICustomerRepository
{
public CustomerRepository(IUnitOfWork unitOfWork)
: base(unitOfWork)
{
}
}
BLL - Business Layer
public class CustomerManager
{
private readonly ICustomerRepository _repository;
public CustomerManager(ICustomerRepository repository)
{
_repository = repository;
}
public IList<Customer> GetAll()
{
return _repository.GetAll<Customer>();
}
}
Presentation - MVC controller
private readonly IUnitOfWork _unitOfWork;
private readonly ICustomerRepository _repository;
private readonly CustomerManager _manager;
public HomeController(IUnitOfWork unitOfWork, ICustomerRepository repository)
{
_unitOfWork = unitOfWork;
_repository = repository;
_manager = new CustomerManager(_repository);
}
I am using IOC container to register unit of work and CustomerRepository objects. The IOC container is working fine.
What is bugging me about this solution is that I have CustomerRepository reference in the controller. Is this architecture fine or there is a better way to implement this?
Upvotes: 3
Views: 708
Reputation: 15890
You shouldn't need/have a IUnitOfWork
or ICustomerRepository
in the controller, with the IoC container you should be able to register the CustomerManager
and then do this:
private readonly CustomerManager _manager;
public HomeController(CustomerManager customerManager)
{
_manager = customerManager;
}
The IoC container should understand the dependency chain and inject all the various dependencies as needed.
I.e. MVC will try and get a HomeController
from the IoC container, which in turn needs a CustomerManager
... so the IoC container will try and get a CustomerManager
, which in turn needs a ICustomerRepository
... so the IoC container will try... you get the point.
I would also go the the extent of extracting an interface from CustomerManager
(ICustomerManager
probably) to remove the coupling and make testing the Controller even easier.
Upvotes: 5
Reputation: 21978
All of your business logic should be in the Manager class, so you shouldn't need a dependency on the Repository in your controller. The controller should just be calling fairly high-level methods in your Manager class.
Upvotes: 1