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