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 { /// /// This is a common base class for CrudAppService and AsyncCrudAppService classes. /// Inherit either from CrudAppService or AsyncCrudAppService, not from this class. /// public abstract class CrudAppServiceBase : ApplicationService where TEntity : class, IEntity where TEntityDto : IEntityDto where TUpdateInput : IEntityDto { protected readonly IRepository 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 repository) { Repository = repository; } /// /// Should apply sorting if needed. /// /// The query. /// The input. protected virtual IQueryable ApplySorting(IQueryable 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; } /// /// Should apply paging if needed. /// /// The query. /// The input. protected virtual IQueryable ApplyPaging(IQueryable 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; } /// /// This method should create based on given input. /// It should filter query if needed, but should not do sorting or paging. /// Sorting should be done in and paging should be done in /// methods. /// /// The input. protected virtual IQueryable CreateFilteredQuery(TGetAllInput input) { return Repository.GetAll(); } /// /// Maps to . /// It uses by default. /// It can be overrided for custom mapping. /// protected virtual TEntityDto MapToEntityDto(TEntity entity) { return ObjectMapper.Map(entity); } /// /// Maps to to create a new entity. /// It uses by default. /// It can be overrided for custom mapping. /// protected virtual TEntity MapToEntity(TCreateInput createInput) { return ObjectMapper.Map(createInput); } /// /// Maps to to update the entity. /// It uses by default. /// It can be overrided for custom mapping. /// 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); } } }