Quantcast
Channel: URF - Unit of Work & (extensible/generic) Repositories Framework
Viewing all articles
Browse latest Browse all 1539

New Post: ServiceLayer

$
0
0
I've thought about the scenario to use UnitOfWork in my service layer. It was quite simple to change your code a little bit and get my desired result. (I hope so)

I've changed the constructor of the service and replaced the following code:
private readonly IRepositoryAsync<TEntity> _repository;
protected Service(IRepositoryAsync<TEntity> repository)
{
             _repository = repository;
}}
with:
private readonly IRepositoryAsync<TEntity> _repository;
protected readonly IUnitOfWorkAsync _unitOfWork; // added UnitOfWork interface
protected Service(IUnitOfWorkAsync unitOfWork) // changed constructor 
{
            _unitOfWork = unitOfWork;
            _repository = unitOfWork.RepositoryAsync<TEntity>(); // calling existing function in unitOfWork to get/create the repository
}
The advantage for me is that I can easily access the repositories and the unitOfWork in my service layer.

In additon i could remove all Unity configurations like:
.RegisterType<IRepositoryAsync<Customer>, Repository<Customer>>()
.RegisterType<IRepositoryAsync<Product>, Repository<Product>>()

Do I have any disadvantages to do it that way?

Viewing all articles
Browse latest Browse all 1539

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>