在AsyncCrudAppService

时间:2018-07-16 13:09:08

标签: asp.net-mvc entity-framework-6 dto aspnetboilerplate application-layer

我在AppServices中使用ABP的AsyncCrudAppService。这是我的界面:

public interface IAssetRequisitionAppService : IAsyncCrudAppService
    <AssetRequisitionDto, Guid, GetAllInput, AssetRequisitionDto, AssetRequisitionDto, AssetRequisitionDetailsDto>
{ }

服务:

public class AssetRequisitionAppService : AsyncCrudAppService
    <AssetRequisition, AssetRequisitionDto, Guid, GetAllInput, AssetRequisitionDto, AssetRequisitionDto, AssetRequisitionDetailsDto>, 
    IAssetRequisitionAppService
{
    public AssetRequisitionAppService(IRepository<AssetRequisition, Guid> repository) : base(repository)
    { }
}

现在,我相信所有这些标准CRUD方法都将返回默认类型(在我的情况下为AssetRequisitionDto)。但是,我想做的是为Get()GetAll()方法返回不同的类型。

Get()应该具有更详细的DTO,并带有Navigation道具的子属性。但是GetAll()的填充表应该不那么详细了。

是否可以通过某种方式覆盖返回类型?

2 个答案:

答案 0 :(得分:0)

是的,有某种方式

// using Microsoft.AspNetCore.Mvc;

[ActionName(nameof(GetAll))]
public PagedResultRequestDto MyGetAll(PagedResultRequestDto input)
{
    return input;
}

[NonAction]
public override Task<PagedResultDto<UserDto>> GetAll(PagedResultRequestDto input)
{
    return base.GetAll(input);
}

参考:https://github.com/aspnetboilerplate/aspnetboilerplate/issues/2859

答案 1 :(得分:0)

好吧,我注意到我最终还是需要更复杂的过滤方法。因此,我创建了自定义类型和方法。

首先,我从GetAllInput派生了自己的PagedAndSortedResultRequestDto。它适合我的大多数服务,因为我通常需要查询与员工和位置有关的数据:

public class GetAllInput : PagedAndSortedResultRequestDto
{
    public long? PersonId { get; set; }
    public long? LocationId { get; set; }
    public EEmployeeType? EmployeeType { get; set; }
}

之后,我为每个服务编写了一个GetAll方法。它们都返回一个PagedResultDto<>,因此我可以在表示层中使用它的功能。下面是一个示例:

//MovableAppService

    public PagedResultDto<MovableLineDto> GetAllLinesRelatedToUser(GetAllInput input)
    {
        Logger.Info("Loading all movables related to current user");

        IQueryable<Movable> queryable = null;
        if (input.PersonId.HasValue)
        {
            if (input.EmployeeType == EEmployeeType.Recorder)
            {
                var recorder = _personRepository.GetAll()
                .OfType<Employee>()
                .FirstOrDefault(x => x.Id == input.PersonId);
                var authorizedStorageIds = recorder.StoragesAuthorized.Select(y => y.Id);

                queryable = _repository.GetAll()
                    .Where(x => authorizedStorageIds.Contains(x.StorageOfOriginId));
            }
            else if (input.EmployeeType == EEmployeeType.UnitManager)
            {
                var locationCodes = _locationRepository.GetAll()
                    .Where(x => x.ManagerInChargeId == input.PersonId)
                    .Select(x => x.Code);

                foreach (var code in locationCodes)
                {
                    queryable = _locationRepository.GetAll()
                        .Where(x => x.Code.StartsWith(code))
                        .SelectMany(x => x.AssignmentDocs)
                        .SelectMany(x => x.Movements)
                        .OfType<Assignment>()
                        .Where(x => x.TimeOfReturn == null)
                        .Select(x => x.Asset)
                        .OfType<Movable>();
                    queryable = queryable.Concat(queryable);
                }
            }
            else if (input.TenantIdsOversee.Count() > 0)
            {
                var overseer = _personRepository.GetAll()
                    .OfType<Overseer>()
                    .FirstOrDefault(x => x.Id == input.PersonId);
                var overseeingTenantIds = overseer.TenantsOversee.Select(y => y.Id);

                queryable = _repository.GetAll()
                   .Where(x => overseeingTenantIds.Contains((int)x.TenantId));
            }
            else if (input.EmployeeType == EEmployeeType.Employee)
            {
                queryable = _personRepository.GetAll()
                    .OfType<Employee>()
                    .Where(x => x.Id == input.PersonId)
                    .SelectMany(x => x.AssignmentDocs)
                    .SelectMany(x => x.Movements)
                    .OfType<Assignment>()
                    .Where(x => x.TimeOfReturn == null)
                    .Select(x => x.Asset)
                    .OfType<Movable>();
            }
        }
        var list = queryable.ToList()
                .OrderBy(x => x.Category.Definition);
        var items = _objectMapper.Map<IReadOnlyList<MovableLineDto>>(list);

        return new PagedResultDto<MovableLineDto>(items.Count, items);
    }

顺便说一句,亚伦的答案可能对 ASP.NET Core 项目有效。但是我的项目在 MVC EF6 中,因此这些注释对我不可用。

现在我将其标记为答案,但是如果有更优雅的方法,我很高兴看到,然后我将更改标记。