using AutoMapper; using DMS.Infrastructure.Repositories; using System.Threading.Tasks; namespace DMS.Infrastructure.Services { /// /// 通用服务基类,封装了常见的增、删、改操作。 /// /// 业务逻辑模型类型。 /// 数据库实体类型。 /// 与实体对应的仓储类型。 public abstract class BaseService where TEntity : class, new() where TRepository : BaseRepository { protected readonly IMapper _mapper; protected readonly TRepository _repository; /// /// 初始化 BaseService 的新实例。 /// /// AutoMapper 实例,用于对象映射。 /// 仓储实例,用于数据访问。 protected BaseService(IMapper mapper, TRepository repository) { _mapper = mapper; _repository = repository; } /// /// 异步添加一个新的业务模型对象。 /// /// 要添加的业务模型对象。 /// 返回添加后的数据库实体。 public virtual async Task AddAsync(TModel model) { var entity = _mapper.Map(model); return await _repository.AddAsync(entity); } /// /// 异步更新一个现有的业务模型对象。 /// /// 要更新的业务模型对象。 /// 返回受影响的行数。 public virtual async Task UpdateAsync(TModel model) { var entity = _mapper.Map(model); return await _repository.UpdateAsync(entity); } /// /// 异步删除一个业务模型对象。 /// /// 要删除的业务模型对象。 /// 返回受影响的行数。 public virtual async Task DeleteAsync(TModel model) { var entity = _mapper.Map(model); return await _repository.DeleteAsync(entity); } } }