覆盖 GetAll 以更改排序 - 需要示例
override GetAll to change Sort - example required
我正在尝试覆盖 AsyncCrudAppService
调用 - "GetAll" 以更改 returned 排序顺序。
首先,重写方法,实际上是正确的方法吗?
如果是的话,我可以举个例子吗,因为我有点不知道如何使用 PagedResultDto<T>
return 类型来做到这一点?
如果没有,请告诉我在哪里可以找到方法。
从技术上讲可以,但如果可以避免,实际上不应该在 GetAll()
中进行排序。 CRUD 应用服务还定义了虚拟方法 ApplySorting()
,它已被 GetAll 使用,也可以被覆盖。
您可以在调用 GetAll()
时将字符串传递给动态排序依据,它已经可以工作了。
await _myAppService.GetAll(new PagedAndSortedResultRequestDto() { Sorting = "Name DESC", MaxResultCount = pageSize, SkipCount = skipCount })
如果您想更好地控制排序行为,例如使用默认值或传入 better-formatted 查询字符串,请覆盖 ApplySorting()
protected override IQueryable<SomeEntity> ApplySorting(IQueryable<SomeEntity> query, PagedAndSortedResultRequestDto input)
{
var sortBy = "LastModificationTime DESC,CreationTime DESC";
switch (input.Sorting?.ToLower())
{
case "name-asc":
sortBy = "Name";
break;
case "name-desc":
sortBy = "Name DESC";
break;
}
input.Sorting = sortBy;
return base.ApplySorting(query, input);
}
我正在尝试覆盖 AsyncCrudAppService
调用 - "GetAll" 以更改 returned 排序顺序。
首先,重写方法,实际上是正确的方法吗?
如果是的话,我可以举个例子吗,因为我有点不知道如何使用 PagedResultDto<T>
return 类型来做到这一点?
如果没有,请告诉我在哪里可以找到方法。
从技术上讲可以,但如果可以避免,实际上不应该在 GetAll()
中进行排序。 CRUD 应用服务还定义了虚拟方法 ApplySorting()
,它已被 GetAll 使用,也可以被覆盖。
您可以在调用 GetAll()
时将字符串传递给动态排序依据,它已经可以工作了。
await _myAppService.GetAll(new PagedAndSortedResultRequestDto() { Sorting = "Name DESC", MaxResultCount = pageSize, SkipCount = skipCount })
如果您想更好地控制排序行为,例如使用默认值或传入 better-formatted 查询字符串,请覆盖 ApplySorting()
protected override IQueryable<SomeEntity> ApplySorting(IQueryable<SomeEntity> query, PagedAndSortedResultRequestDto input)
{
var sortBy = "LastModificationTime DESC,CreationTime DESC";
switch (input.Sorting?.ToLower())
{
case "name-asc":
sortBy = "Name";
break;
case "name-desc":
sortBy = "Name DESC";
break;
}
input.Sorting = sortBy;
return base.ApplySorting(query, input);
}