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:
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?
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?