AsyncCrudAppService.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. using System.Linq;
  2. using System.Threading.Tasks;
  3. using Abp.Application.Services.Dto;
  4. using Abp.Domain.Entities;
  5. using Abp.Domain.Repositories;
  6. using Abp.Linq;
  7. namespace Abp.Application.Services
  8. {
  9. public abstract class AsyncCrudAppService<TEntity, TEntityDto>
  10. : AsyncCrudAppService<TEntity, TEntityDto, int>
  11. where TEntity : class, IEntity<int>
  12. where TEntityDto : IEntityDto<int>
  13. {
  14. protected AsyncCrudAppService(IRepository<TEntity, int> repository)
  15. : base(repository)
  16. {
  17. }
  18. }
  19. public abstract class AsyncCrudAppService<TEntity, TEntityDto, TPrimaryKey>
  20. : AsyncCrudAppService<TEntity, TEntityDto, TPrimaryKey, PagedAndSortedResultRequestDto>
  21. where TEntity : class, IEntity<TPrimaryKey>
  22. where TEntityDto : IEntityDto<TPrimaryKey>
  23. {
  24. protected AsyncCrudAppService(IRepository<TEntity, TPrimaryKey> repository)
  25. : base(repository)
  26. {
  27. }
  28. }
  29. public abstract class AsyncCrudAppService<TEntity, TEntityDto, TPrimaryKey, TGetAllInput>
  30. : AsyncCrudAppService<TEntity, TEntityDto, TPrimaryKey, TGetAllInput, TEntityDto, TEntityDto>
  31. where TEntity : class, IEntity<TPrimaryKey>
  32. where TEntityDto : IEntityDto<TPrimaryKey>
  33. {
  34. protected AsyncCrudAppService(IRepository<TEntity, TPrimaryKey> repository)
  35. : base(repository)
  36. {
  37. }
  38. }
  39. public abstract class AsyncCrudAppService<TEntity, TEntityDto, TPrimaryKey, TGetAllInput, TCreateInput>
  40. : AsyncCrudAppService<TEntity, TEntityDto, TPrimaryKey, TGetAllInput, TCreateInput, TCreateInput>
  41. where TGetAllInput : IPagedAndSortedResultRequest
  42. where TEntity : class, IEntity<TPrimaryKey>
  43. where TEntityDto : IEntityDto<TPrimaryKey>
  44. where TCreateInput : IEntityDto<TPrimaryKey>
  45. {
  46. protected AsyncCrudAppService(IRepository<TEntity, TPrimaryKey> repository)
  47. : base(repository)
  48. {
  49. }
  50. }
  51. public abstract class AsyncCrudAppService<TEntity, TEntityDto, TPrimaryKey, TGetAllInput, TCreateInput, TUpdateInput>
  52. : AsyncCrudAppService<TEntity, TEntityDto, TPrimaryKey, TGetAllInput, TCreateInput, TUpdateInput, EntityDto<TPrimaryKey>>
  53. where TEntity : class, IEntity<TPrimaryKey>
  54. where TEntityDto : IEntityDto<TPrimaryKey>
  55. where TUpdateInput : IEntityDto<TPrimaryKey>
  56. {
  57. protected AsyncCrudAppService(IRepository<TEntity, TPrimaryKey> repository)
  58. : base(repository)
  59. {
  60. }
  61. }
  62. public abstract class AsyncCrudAppService<TEntity, TEntityDto, TPrimaryKey, TGetAllInput, TCreateInput, TUpdateInput, TGetInput>
  63. : AsyncCrudAppService<TEntity, TEntityDto, TPrimaryKey, TGetAllInput, TCreateInput, TUpdateInput, TGetInput, EntityDto<TPrimaryKey>>
  64. where TEntity : class, IEntity<TPrimaryKey>
  65. where TEntityDto : IEntityDto<TPrimaryKey>
  66. where TUpdateInput : IEntityDto<TPrimaryKey>
  67. where TGetInput : IEntityDto<TPrimaryKey>
  68. {
  69. protected AsyncCrudAppService(IRepository<TEntity, TPrimaryKey> repository)
  70. : base(repository)
  71. {
  72. }
  73. }
  74. public abstract class AsyncCrudAppService<TEntity, TEntityDto, TPrimaryKey, TGetAllInput, TCreateInput, TUpdateInput, TGetInput, TDeleteInput>
  75. : CrudAppServiceBase<TEntity, TEntityDto, TPrimaryKey, TGetAllInput, TCreateInput, TUpdateInput>,
  76. IAsyncCrudAppService<TEntityDto, TPrimaryKey, TGetAllInput, TCreateInput, TUpdateInput, TGetInput, TDeleteInput>
  77. where TEntity : class, IEntity<TPrimaryKey>
  78. where TEntityDto : IEntityDto<TPrimaryKey>
  79. where TUpdateInput : IEntityDto<TPrimaryKey>
  80. where TGetInput : IEntityDto<TPrimaryKey>
  81. where TDeleteInput : IEntityDto<TPrimaryKey>
  82. {
  83. public IAsyncQueryableExecuter AsyncQueryableExecuter { get; set; }
  84. protected AsyncCrudAppService(IRepository<TEntity, TPrimaryKey> repository)
  85. :base(repository)
  86. {
  87. AsyncQueryableExecuter = NullAsyncQueryableExecuter.Instance;
  88. }
  89. public virtual async Task<TEntityDto> Get(TGetInput input)
  90. {
  91. CheckGetPermission();
  92. var entity = await GetEntityByIdAsync(input.Id);
  93. return MapToEntityDto(entity);
  94. }
  95. public virtual async Task<PagedResultDto<TEntityDto>> GetAll(TGetAllInput input)
  96. {
  97. CheckGetAllPermission();
  98. var query = CreateFilteredQuery(input);
  99. var totalCount = await AsyncQueryableExecuter.CountAsync(query);
  100. query = ApplySorting(query, input);
  101. query = ApplyPaging(query, input);
  102. var entities = await AsyncQueryableExecuter.ToListAsync(query);
  103. return new PagedResultDto<TEntityDto>(
  104. totalCount,
  105. entities.Select(MapToEntityDto).ToList()
  106. );
  107. }
  108. public virtual async Task<TEntityDto> Create(TCreateInput input)
  109. {
  110. CheckCreatePermission();
  111. var entity = MapToEntity(input);
  112. await Repository.InsertAsync(entity);
  113. await CurrentUnitOfWork.SaveChangesAsync();
  114. return MapToEntityDto(entity);
  115. }
  116. public virtual async Task<TEntityDto> Update(TUpdateInput input)
  117. {
  118. CheckUpdatePermission();
  119. var entity = await GetEntityByIdAsync(input.Id);
  120. MapToEntity(input, entity);
  121. await CurrentUnitOfWork.SaveChangesAsync();
  122. return MapToEntityDto(entity);
  123. }
  124. public virtual Task Delete(TDeleteInput input)
  125. {
  126. CheckDeletePermission();
  127. return Repository.DeleteAsync(input.Id);
  128. }
  129. protected virtual Task<TEntity> GetEntityByIdAsync(TPrimaryKey id)
  130. {
  131. return Repository.GetAsync(id);
  132. }
  133. }
  134. }