Service with multiple repositories

Apr 13, 2015 at 1:44 PM
Edited Apr 13, 2015 at 1:46 PM
I have a business logic that involves querying to multiple tables. For instance, the validation to insert a Customer will check for something also in Category table too.

How do we accomplish this is this framework? (I didn't see any example)
  • Do he have to inject all the repositories in CustomerService ?
private readonly IRepositoryAsync<Customer> _repository;
private readonly IRepositoryAsync<Category> _categoryRepository;

public CustomerService(IRepositoryAsync<Customer> repository, IRepositoryAsync<Category> categoryRepository): base(repository)
        _repository = repository;
        _categoryRepository = categoryRepository;
  • Or this framework involves only 1:1 service and repository relation?
May 12, 2015 at 5:46 PM
Yes, best practice is to inject all needed repositories into your Service, so that's there is some context of what the Service's intent is by looking at whats injected. However if you inject IUnitOfWork you can access all repositories by IUnitOfWork.Repository<Customer>() (least preferred).
Marked as answer by lelong37 on 5/12/2015 at 9:46 AM