| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166 |
- using System.Linq;
- using System.Linq.Dynamic.Core;
- using Abp.Application.Services.Dto;
- using Abp.Authorization;
- using Abp.Domain.Entities;
- using Abp.Domain.Repositories;
- using Abp.Extensions;
- using Abp.Linq.Extensions;
- using Abp.ObjectMapping;
- namespace Abp.Application.Services
- {
- /// <summary>
- /// This is a common base class for CrudAppService and AsyncCrudAppService classes.
- /// Inherit either from CrudAppService or AsyncCrudAppService, not from this class.
- /// </summary>
- public abstract class CrudAppServiceBase<TEntity, TEntityDto, TPrimaryKey, TGetAllInput, TCreateInput, TUpdateInput> : ApplicationService
- where TEntity : class, IEntity<TPrimaryKey>
- where TEntityDto : IEntityDto<TPrimaryKey>
- where TUpdateInput : IEntityDto<TPrimaryKey>
- {
- protected readonly IRepository<TEntity, TPrimaryKey> Repository;
- protected virtual string GetPermissionName { get; set; }
- protected virtual string GetAllPermissionName { get; set; }
- protected virtual string CreatePermissionName { get; set; }
- protected virtual string UpdatePermissionName { get; set; }
- protected virtual string DeletePermissionName { get; set; }
- protected CrudAppServiceBase(IRepository<TEntity, TPrimaryKey> repository)
- {
- Repository = repository;
- }
- /// <summary>
- /// Should apply sorting if needed.
- /// </summary>
- /// <param name="query">The query.</param>
- /// <param name="input">The input.</param>
- protected virtual IQueryable<TEntity> ApplySorting(IQueryable<TEntity> query, TGetAllInput input)
- {
- //Try to sort query if available
- var sortInput = input as ISortedResultRequest;
- if (sortInput != null)
- {
- if (!sortInput.Sorting.IsNullOrWhiteSpace())
- {
- return query.OrderBy(sortInput.Sorting);
- }
- }
- //IQueryable.Task requires sorting, so we should sort if Take will be used.
- if (input is ILimitedResultRequest)
- {
- return query.OrderByDescending(e => e.Id);
- }
- //No sorting
- return query;
- }
- /// <summary>
- /// Should apply paging if needed.
- /// </summary>
- /// <param name="query">The query.</param>
- /// <param name="input">The input.</param>
- protected virtual IQueryable<TEntity> ApplyPaging(IQueryable<TEntity> query, TGetAllInput input)
- {
- //Try to use paging if available
- var pagedInput = input as IPagedResultRequest;
- if (pagedInput != null)
- {
- return query.PageBy(pagedInput);
- }
- //Try to limit query result if available
- var limitedInput = input as ILimitedResultRequest;
- if (limitedInput != null)
- {
- return query.Take(limitedInput.MaxResultCount);
- }
- //No paging
- return query;
- }
- /// <summary>
- /// This method should create <see cref="IQueryable{TEntity}"/> based on given input.
- /// It should filter query if needed, but should not do sorting or paging.
- /// Sorting should be done in <see cref="ApplySorting"/> and paging should be done in <see cref="ApplyPaging"/>
- /// methods.
- /// </summary>
- /// <param name="input">The input.</param>
- protected virtual IQueryable<TEntity> CreateFilteredQuery(TGetAllInput input)
- {
- return Repository.GetAll();
- }
- /// <summary>
- /// Maps <see cref="TEntity"/> to <see cref="TEntityDto"/>.
- /// It uses <see cref="IObjectMapper"/> by default.
- /// It can be overrided for custom mapping.
- /// </summary>
- protected virtual TEntityDto MapToEntityDto(TEntity entity)
- {
- return ObjectMapper.Map<TEntityDto>(entity);
- }
- /// <summary>
- /// Maps <see cref="TEntityDto"/> to <see cref="TEntity"/> to create a new entity.
- /// It uses <see cref="IObjectMapper"/> by default.
- /// It can be overrided for custom mapping.
- /// </summary>
- protected virtual TEntity MapToEntity(TCreateInput createInput)
- {
- return ObjectMapper.Map<TEntity>(createInput);
- }
- /// <summary>
- /// Maps <see cref="TUpdateInput"/> to <see cref="TEntity"/> to update the entity.
- /// It uses <see cref="IObjectMapper"/> by default.
- /// It can be overrided for custom mapping.
- /// </summary>
- protected virtual void MapToEntity(TUpdateInput updateInput, TEntity entity)
- {
- ObjectMapper.Map(updateInput, entity);
- }
- protected virtual void CheckPermission(string permissionName)
- {
- if (!string.IsNullOrEmpty(permissionName))
- {
- PermissionChecker.Authorize(permissionName);
- }
- }
- protected virtual void CheckGetPermission()
- {
- CheckPermission(GetPermissionName);
- }
- protected virtual void CheckGetAllPermission()
- {
- CheckPermission(GetAllPermissionName);
- }
- protected virtual void CheckCreatePermission()
- {
- CheckPermission(CreatePermissionName);
- }
- protected virtual void CheckUpdatePermission()
- {
- CheckPermission(UpdatePermissionName);
- }
- protected virtual void CheckDeletePermission()
- {
- CheckPermission(DeletePermissionName);
- }
- }
- }
|