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();
}