Allow for sorting when doing paginated requests
See original GitHub issueIs your feature request related to a problem? Please describe.
We need to be able to sort paginated queries. I see this as a critical feature needed for paging. Lists usually needs to be sorted and when the requests is paginated that the needs to be done serverside otherwise we always need to get all of the data, sort it and then page it, instead of letting the cosmos db serve the paginated response.
I’m not sure if I have missed something in the interface but apart from direct SQL queries I don’t see a possibility to do sorting when using PageAsync. And if I understood correctly doing direct queries will ruin the possibility to use the in memory database for testing. Which is a really nice feature to have.
Describe the solution you’d like The biggest question for this feature is how update the IRepository interface to include sorting.
My suggestion would be to look at the specification pattern that can be seen here:
https://github.com/ardalis/Specification
But limit it to whats needed here, since the ardalis specification implementation is sql + EF specific. This would also allow for a scalable way to implement more features in the future. This is what I think would be needed for a specification pattern that’s not too complex to implement and would give value right now:
//Setup classes
public interface ISpecification<T>
{
IEnumerable<Expression<Func<T, bool>> WhereExpressions { get; }
IEnumerable<Expression<Func<T, object?>>, OrderTypeEnum> OrderExpressions { get; }
int? Take { get; }
int? Skip { get; }
string? ContinuationToken{ get; }
}
public enum OrderTypeEnum
{
OrderBy = 1,
OrderByDescending = 2,
ThenBy = 3,
ThenByDescending = 4
}
//Page method:
public async ValueTask<IPageQueryResult<TItem>> PageAsync(
ISpecification<TItem>,
CancellationToken cancellationToken = default)
Describe alternatives you’ve considered
It is also possible to extend the page method to include the order by clause:
IEnumerable<Expression<Func<T, object?>>
But I don’t think this is a scaleable way to keep developing the repository. It will probably lead to a method that just keeps growing in size.
Another solution is to got the IQueryable path but I agree with the conclussions reached here: https://github.com/IEvangelist/azure-cosmos-dotnet-repository/issues/88 that opening the door to IQueryable will add lots of complexity for the small subset of features thats needed.
Additional context If there currently is another way to sort results for a paged query please let me know, I have tried to look everyone but then I missed it.
Issue Analytics
- State:
- Created 2 years ago
- Reactions:1
- Comments:16 (8 by maintainers)
Ok I’ll start looking at making PR for this
Yeah, let’s see what the PR looks like and go from there. I’m good either way.