I want to use Shared Service that include some common methods like create, update, delete in my business layer. I have already implemented repository and unit of work classes but I encountered some problems while trying to create a shared service. Lets assume that we have a non-shared create method like this:
public class ProductService : IProductService
{
private readonly IUnitOfWork _unitOfWork;
public ProductService(IUnitOfWork unitOfWork)
{
_unitOfWork = unitOfWork;
}
public async Task<Product> CreateProduct(Product newProduct)
{
await _unitOfWork.Products.AddAsync(newProduct);
await _unitOfWork.CommitAsync();
return newProduct;
}
}
The part that confuses me in the above code is, I call my UnitOfWork with _unitOfWork.Product command, how do we convert it to unitOfWork.TEntity to make generic? Is it possible? In this case, I tried doing generic but I guess there is no such thing as _unitOfWork.TEntity. I just need to edit the Service class, I add other related classes to give extra information.
Service:
public class Service<TEntity>: IService<TEntity> where TEntity : class
{
private readonly IUnitOfWork _unitOfWork;
public Service(IUnitOfWork unitOfWork)
{
_unitOfWork = unitOfWork;
}
public async Task<TEntity> AddAsync(TEntity entity)
{
await _unitOfWork.TEntity.AddAsync(entity);
await _unitOfWork.CommitAsync();
return entity;
}
}
IService:
public interface IService<TEntity> where TEntity : class
{
Task<TEntity> AddAsync(TEntity entity);
}
Repository:
public abstract class Repository<TEntity> : IRepository<TEntity> where TEntity : class
{
private readonly DbContext _context;
private readonly DbSet<TEntity> _dbSet;
public Repository(ECommerceDbContext context)
{
_context = context;
_dbSet = context.Set<TEntity>();
}
public async Task<TEntity> AddAsync(TEntity entity)
{
entity.CreateDate = DateTime.Now;
await _dbSet.AddAsync(entity);
await SaveAsync();
return entity;
}
public async Task AddAsync(IEnumerable<TEntity> entities)
{
foreach (var item in entities)
{
await AddAsync(item);
}
}
public async Task<bool> AnyAsync(Expression<Func<TEntity, bool>> expression)
{
return await _dbSet.AnyAsync(expression);
}
public async Task<bool> AnyAsync()
{
return await AnyAsync(x => true);
}
public async Task<long> CountAsync()
{
return await CountAsync(x => true);
}
public async Task<long> CountAsync(Expression<Func<TEntity, bool>> expression)
{
return await _dbSet.LongCountAsync(expression);
}
public void Delete(TEntity model)
{
_dbSet.Remove(model);
}
public async Task DeleteAsync(int id)
{
var entity = await GetAsync(id);
Delete(entity);
}
public async Task<TEntity> FirstOrDefault(Expression<Func<TEntity, bool>> expression)
{
return await _dbSet.FirstOrDefaultAsync(expression);
}
public async Task<TEntity> GetAsync(int id)
{
return await FirstOrDefault(x => x.Id == id);
}
public async Task<IEnumerable<TEntity>> GetAll()
{
return await _dbSet.ToListAsync();
}
public async Task SaveAsync()
{
await _context.SaveChangesAsync();
}
public async Task<TEntity> Update(TEntity entity)
{
var temp = GetAsync(entity.Id);
entity.UpdateDate = DateTime.Now;
_context.Entry(temp).CurrentValues.SetValues(entity);
await SaveAsync();
return await temp;
}
}
IRepository:
public interface IRepository<TEntity> where TEntity :class
{
Task<TEntity> AddAsync(TEntity entity);
Task AddAsync(IEnumerable<TEntity> entities);
Task<bool> AnyAsync(Expression<Func<TEntity, bool>> expression);
Task<bool> AnyAsync();
Task<long> CountAsync();
Task<long> CountAsync(Expression<Func<TEntity, bool>> expression);
void Delete(TEntity model);
Task DeleteAsync(int id);
Task<TEntity> FirstOrDefault(Expression<Func<TEntity, bool>> expression);
Task<TEntity> GetAsync(int id);
Task<IEnumerable<TEntity>> GetAll();
Task SaveAsync();
Task<TEntity> Update(TEntity entity);
}
UnitOfWork:
public class UnitOfWork:IUnitOfWork
{
private readonly ECommerceDbContext _context;
private ProductRepository _productRepository;
public UnitOfWork(ECommerceDbContext context)
{
_context = context;
}
public IProductRepository Products => _productRepository = _productRepository ?? new ProductRepository(_context);
public async Task<int> CommitAsync()
{
return await _context.SaveChangesAsync();
}
public void Dispose()
{
_context.Dispose();
}
}
IUnitOfWork
public interface IUnitOfWork : IDisposable
{
IProductRepository Products { get; }
Task<int> CommitAsync();
}
Service TEntity: class.. I think here class is TEntity: Entity.
It's helpful if you share your 'IProductRepository' and ProductRepository objects.
public interface IService<TEntity> where TEntity : Entity
{
Task<TEntity> AddAsync(TEntity entity);
}
public class Service<TEntity> : IService<TEntity> where TEntity : Entity
{
private readonly IUnitOfWork _unitOfWork;
public Service(IUnitOfWork unitOfWork)
{
_unitOfWork = unitOfWork;
}
public async Task<TEntity> AddAsync(TEntity entity)
{
//await _unitOfWork.TEntity.AddAsync(entity);
await _unitOfWork.Products.AddAsync(entity);
await _unitOfWork.CommitAsync();
return entity;
}
}
public class ProductService : Service<Product>,IProductService
{
public ProductService(IUnitOfWork unitOfWork):base(unitOfWork)
{
}
}
public class UnitOfWork : IUnitOfWork
{
private readonly ECommerceDbContext _context;
private IProductRepository _productRepository;
public UnitOfWork(ECommerceDbContext context)
{
_context = context;
}
public IProductRepository Products => _productRepository = _productRepository ?? new ProductRepository(_context);
public async Task<int> CommitAsync()
{
return await _context.SaveChangesAsync();
}
public void Dispose()
{
_context.Dispose();
}
}
Because each entity type is a strongly typed object, you cannot genericize your implementation without reflection. A common workaround is to provide a generic callback while allowing the invoker to provide the specific mapping per entity type.
public class Service<TEntity> : IService<TEntity> where TEntity : class
{
private readonly IUnitOfWork _unitOfWork;
public Service(IUnitOfWork unitOfWork)
{
_unitOfWork = unitOfWork;
}
public async Task<TEntity> AddAsync(TEntity entity,
Func<TEntity, IUnitOfWork, Task> addEntityCallback)
{
await addEntityCallback(entity, _unitOfWork);
await _unitOfWork.CommitAsync();
return entity;
}
}
public interface IService<TEntity> where TEntity : class
{
Task<TEntity> AddAsync(TEntity entity, Func<TEntity, IUnitOfWork, Task> addEntityCallback);
}
You can then call IService.AddAsync
with a specific mapping:
public class ProductService : IProductService
{
private readonly IService<Product> _service;
public ProductService(IService<Product> service)
{
_service = service;
}
public async Task<Product> CreateProduct(Product newProduct)
{
await _service.AddAsync(newProduct,
(entity, unitOfWork) => unitOfWork.Products.AddAsync(entity));
return newProduct;
}
}
Update: in the case where you want to always inherit Service<TEntity>
(per your comment), you could use an abstract method that functions in a similar manner to a callback parameter. This allows you to still encapsulate the logic in the ProductService
but now no longer requires you to provide a create method.
public class ProductService : Service<Product>, IProductService
{
public ProductService(IUnitOfWork unitOfWork) : base(unitOfWork)
{
}
protected override async Task<Product> AddEntityCallback(Product entity,
IUnitOfWork unitOfWork)
{
await unitOfWork.Products.AddAsync(entity);
return entity;
}
}
public abstract class Service<TEntity> : IService<TEntity> where TEntity : class
{
private readonly IUnitOfWork _unitOfWork;
public Service(IUnitOfWork unitOfWork)
{
_unitOfWork = unitOfWork;
}
protected abstract Task<TEntity> AddEntityCallback(TEntity entity,
IUnitOfWork unitOfWork);
public async Task<TEntity> AddAsync(TEntity entity)
{
await AddEntityCallback(entity, _unitOfWork);
await _unitOfWork.CommitAsync();
return entity;
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.