jeudi 30 avril 2015

Autofac Repository Pattern and Unit Of Work

I have searched a little bit and I am confused.

Should I have a repository for each entity or one generic repository for all of them ?

I use Autofac. Generic repository and unit of work class can share same context. Therefore, Does unit of work class have to have generic repository ? Is this anti pattern ?

Can I use extension classes or service layer instead of using other repositories for extension methods which is not shared on generic repository?

First Approach uses a repository and a service for each entity. Unit of work class does not have repositories. Therefore, you should create each repository instead of just creating one unit of work class in caller constructor.

HomeController(IUnitOfWork unitOfWork, IUserRepository userRepository,IOrderRepository orderRepository,IBalanceRepository balanceRepository)

Second Approach uses just a generic repository. It uses extension classes instead of using one repository for each entity. Unit of work class has generic repositories.Therefore, you can just create a unit of work class on caller class constructor.

public HomeController(IUnitOfWork unitOfWork)

In this approach we use one generic class for repositories but we create an object for each entity. If this approach is fine how can I implement it with Autofac ?

Third Approach uses one generic repository and one object for generic repository. It uses generic methods instead of generic class. But generic repository has unit of work class instead of opposite.

public HomeController(IUnitOfWork unitOfWork,IGenericRepository repository)

Which approach should I use ?

Aucun commentaire:

Enregistrer un commentaire