I am trying to override an AsyncCrudAppService
call - "GetAll" in order to change the returned sort order.
Firstly, is overriding the method, in fact, the correct way to do this?
If yes, could I have an example as I'm a bit lost on how to do this with a PagedResultDto<T>
return type?
If not please let me know where I can find out how.
Technically you can, but really shouldn't do sorting in GetAll()
if it can be avoided. CRUD App Services also define virtual method ApplySorting()
which is already used by GetAll and can also be overridden.
You can pass the string to dynamically sort by when calling the GetAll()
and it will already work.
await _myAppService.GetAll(new PagedAndSortedResultRequestDto() { Sorting = "Name DESC", MaxResultCount = pageSize, SkipCount = skipCount })
If you'd like to have more control over sorting behavior, like use something default or pass in better-formatted query strings, override 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);
}