CrudAppServiceBase.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. using System.Linq;
  2. using System.Linq.Dynamic.Core;
  3. using Abp.Application.Services.Dto;
  4. using Abp.Authorization;
  5. using Abp.Domain.Entities;
  6. using Abp.Domain.Repositories;
  7. using Abp.Extensions;
  8. using Abp.Linq.Extensions;
  9. using Abp.ObjectMapping;
  10. namespace Abp.Application.Services
  11. {
  12. /// <summary>
  13. /// This is a common base class for CrudAppService and AsyncCrudAppService classes.
  14. /// Inherit either from CrudAppService or AsyncCrudAppService, not from this class.
  15. /// </summary>
  16. public abstract class CrudAppServiceBase<TEntity, TEntityDto, TPrimaryKey, TGetAllInput, TCreateInput, TUpdateInput> : ApplicationService
  17. where TEntity : class, IEntity<TPrimaryKey>
  18. where TEntityDto : IEntityDto<TPrimaryKey>
  19. where TUpdateInput : IEntityDto<TPrimaryKey>
  20. {
  21. protected readonly IRepository<TEntity, TPrimaryKey> Repository;
  22. protected virtual string GetPermissionName { get; set; }
  23. protected virtual string GetAllPermissionName { get; set; }
  24. protected virtual string CreatePermissionName { get; set; }
  25. protected virtual string UpdatePermissionName { get; set; }
  26. protected virtual string DeletePermissionName { get; set; }
  27. protected CrudAppServiceBase(IRepository<TEntity, TPrimaryKey> repository)
  28. {
  29. Repository = repository;
  30. }
  31. /// <summary>
  32. /// Should apply sorting if needed.
  33. /// </summary>
  34. /// <param name="query">The query.</param>
  35. /// <param name="input">The input.</param>
  36. protected virtual IQueryable<TEntity> ApplySorting(IQueryable<TEntity> query, TGetAllInput input)
  37. {
  38. //Try to sort query if available
  39. var sortInput = input as ISortedResultRequest;
  40. if (sortInput != null)
  41. {
  42. if (!sortInput.Sorting.IsNullOrWhiteSpace())
  43. {
  44. return query.OrderBy(sortInput.Sorting);
  45. }
  46. }
  47. //IQueryable.Task requires sorting, so we should sort if Take will be used.
  48. if (input is ILimitedResultRequest)
  49. {
  50. return query.OrderByDescending(e => e.Id);
  51. }
  52. //No sorting
  53. return query;
  54. }
  55. /// <summary>
  56. /// Should apply paging if needed.
  57. /// </summary>
  58. /// <param name="query">The query.</param>
  59. /// <param name="input">The input.</param>
  60. protected virtual IQueryable<TEntity> ApplyPaging(IQueryable<TEntity> query, TGetAllInput input)
  61. {
  62. //Try to use paging if available
  63. var pagedInput = input as IPagedResultRequest;
  64. if (pagedInput != null)
  65. {
  66. return query.PageBy(pagedInput);
  67. }
  68. //Try to limit query result if available
  69. var limitedInput = input as ILimitedResultRequest;
  70. if (limitedInput != null)
  71. {
  72. return query.Take(limitedInput.MaxResultCount);
  73. }
  74. //No paging
  75. return query;
  76. }
  77. /// <summary>
  78. /// This method should create <see cref="IQueryable{TEntity}"/> based on given input.
  79. /// It should filter query if needed, but should not do sorting or paging.
  80. /// Sorting should be done in <see cref="ApplySorting"/> and paging should be done in <see cref="ApplyPaging"/>
  81. /// methods.
  82. /// </summary>
  83. /// <param name="input">The input.</param>
  84. protected virtual IQueryable<TEntity> CreateFilteredQuery(TGetAllInput input)
  85. {
  86. return Repository.GetAll();
  87. }
  88. /// <summary>
  89. /// Maps <see cref="TEntity"/> to <see cref="TEntityDto"/>.
  90. /// It uses <see cref="IObjectMapper"/> by default.
  91. /// It can be overrided for custom mapping.
  92. /// </summary>
  93. protected virtual TEntityDto MapToEntityDto(TEntity entity)
  94. {
  95. return ObjectMapper.Map<TEntityDto>(entity);
  96. }
  97. /// <summary>
  98. /// Maps <see cref="TEntityDto"/> to <see cref="TEntity"/> to create a new entity.
  99. /// It uses <see cref="IObjectMapper"/> by default.
  100. /// It can be overrided for custom mapping.
  101. /// </summary>
  102. protected virtual TEntity MapToEntity(TCreateInput createInput)
  103. {
  104. return ObjectMapper.Map<TEntity>(createInput);
  105. }
  106. /// <summary>
  107. /// Maps <see cref="TUpdateInput"/> to <see cref="TEntity"/> to update the entity.
  108. /// It uses <see cref="IObjectMapper"/> by default.
  109. /// It can be overrided for custom mapping.
  110. /// </summary>
  111. protected virtual void MapToEntity(TUpdateInput updateInput, TEntity entity)
  112. {
  113. ObjectMapper.Map(updateInput, entity);
  114. }
  115. protected virtual void CheckPermission(string permissionName)
  116. {
  117. if (!string.IsNullOrEmpty(permissionName))
  118. {
  119. PermissionChecker.Authorize(permissionName);
  120. }
  121. }
  122. protected virtual void CheckGetPermission()
  123. {
  124. CheckPermission(GetPermissionName);
  125. }
  126. protected virtual void CheckGetAllPermission()
  127. {
  128. CheckPermission(GetAllPermissionName);
  129. }
  130. protected virtual void CheckCreatePermission()
  131. {
  132. CheckPermission(CreatePermissionName);
  133. }
  134. protected virtual void CheckUpdatePermission()
  135. {
  136. CheckPermission(UpdatePermissionName);
  137. }
  138. protected virtual void CheckDeletePermission()
  139. {
  140. CheckPermission(DeletePermissionName);
  141. }
  142. }
  143. }