CrudAppService.cs 5.3 KB

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