using AutoMapper; using Microsoft.Extensions.DependencyInjection; using System; using System.Collections.Generic; using System.Linq; using System.Linq.Expressions; using System.Security.Principal; using System.Text; using System.Threading.Tasks; using Yi.Framework.Common.Attribute; using Yi.Framework.Common.Enum; using Yi.Framework.Common.Exceptions; using Yi.Framework.Interface.Base.Crud; using Yi.Framework.Model.Base; using Yi.Framework.Repository; namespace Yi.Framework.Service.Base.Crud { public abstract class AbstractKeyReadOnlyAppService : AbstractKeyReadOnlyAppService where TEntity : class, IEntity, new() { } public abstract class AbstractKeyReadOnlyAppService : ApplicationService, IReadOnlyAppService where TEntity : class, IEntity, new() { [Autowired] public IRepository Repository { get; set; } public async Task> GetListAsync() { var entitys = await Repository.GetListAsync(); var entityDtos = await MapToGetListOutputDtosAsync(entitys); return entityDtos; } public async Task GetByIdAsync(TKey id) { var entity = await GetEntityByIdAsync(id); if (entity is null) { throw new UserFriendlyException($"主键:{id} 数据不存在",ResultCodeEnum.NotSuccess); } var entityDto = await MapToGetOutputDtoAsync(entity); return entityDto; } /// /// 通过id获取实体 /// /// /// protected abstract Task GetEntityByIdAsync(TKey id); /// /// 实体向Get输出映射的异步方法 /// /// /// protected virtual Task MapToGetOutputDtoAsync(TEntity entity) { return Task.FromResult(MapToGetOutputDto(entity)); } /// /// 实体向Get输出Dto映射的同步方法 /// /// /// protected virtual TGetOutputDto MapToGetOutputDto(TEntity entity) { return ObjectMapper.Map(entity); } /// /// 多个实体列表映射GetList输出dto列表的异步方法 /// /// /// protected virtual async Task> MapToGetListOutputDtosAsync(IEnumerable entities) { var dtos = new List(); foreach (var entity in entities) { dtos.Add(await MapToGetListOutputDtoAsync(entity)); } return dtos; } /// /// 多个实体列表映射GetList输出dto列表的同步方法 /// /// /// protected virtual async Task> MapToGetListOutputDtos(IEnumerable entities) { var dtos = new List(); foreach (var entity in entities) { dtos.Add(await MapToGetListOutputDtoAsync(entity)); } return dtos; } /// /// 实体列表映射GetList输出dto的异步方法 /// /// /// protected virtual Task MapToGetListOutputDtoAsync(TEntity entity) { return Task.FromResult(MapToGetListOutputDto(entity)); } /// /// 实体列表映射GetList输出dto的同步方法 /// /// /// protected virtual TGetListOutputDto MapToGetListOutputDto(TEntity entity) { return ObjectMapper.Map(entity); } } }