asp.net-mvc - 更改 AsyncCrudAppService 中 GetAll 方法的输出 DTO(返回值)
问题描述
我AsyncCrudAppService
在我的 AppServices 中使用 ABP。这是我的界面:
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()
应该有一个不那么详细的,只是为了填充一个表格。
有没有办法以某种方式覆盖返回类型?
解决方案
好吧,我注意到我最终还是需要更复杂的过滤方法。所以,我创建了我的自定义类型和方法。
首先,我创建了自己的GetAllInput
派生自PagedAndSortedResultRequestDto
. 它适用于我的大部分服务,因为我通常需要查询与员工和位置相关的数据:
public class GetAllInput : PagedAndSortedResultRequestDto
{
public long? PersonId { get; set; }
public long? LocationId { get; set; }
public EEmployeeType? EmployeeType { get; set; }
}
之后,我GetAll
为我的每项服务编写了一个方法。它们都返回 aPagedResultDto<>
所以我可以在表示层中使用它的功能。下面是一个示例:
//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中,所以这些注释对我不可用。
现在我将此标记为答案,但如果有更优雅的方式,我很高兴看到,然后我会改变我的标记。
推荐阅读
- python - 连续读取文件并将新行添加到列表(python)
- elasticsearch - 匹配 ELK 中特定位置的子字符串电子邮件地址
- docker - 为什么在 azure pipline 上拉取基本图像需要很长时间?
- azure-data-factory-2 - Azure Storage V2 blob 事件 - 不触发 ADF
- javascript - Chrome 扩展对 getHardwarePlatformInfo 的调用返回“不允许”错误
- node.js - 如何在代码级别生成 mpd(MPEG Dash 清单)文件
- selenium-webdriver - 不使用选择类我想确保“sam”选项在下拉列表中不可用
- laravel - 运行 NPM 安装失败
- git - 如何使用 git svn 从本地共享存储库克隆
- javascript - 如何使用 chai 在量角器中断言 sendkeys?