question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Allow for sorting when doing paginated requests

See original GitHub issue

Is 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:closed
  • Created 2 years ago
  • Reactions:1
  • Comments:16 (8 by maintainers)

github_iconTop GitHub Comments

2reactions
filipmhperssoncommented, Jan 12, 2022

Ok I’ll start looking at making PR for this

1reaction
IEvangelistcommented, Jan 11, 2022

Yeah, let’s see what the PR looks like and go from there. I’m good either way.

Read more comments on GitHub >

github_iconTop Results From Across the Web

web applications - Issues with pagination and sorting
The best approach would be to do the sorting and paging at the database level and return only a subset of the original...
Read more >
REST API Design: Filtering, Sorting, and Pagination
To enable sorting, many APIs add a sort or sort_by URL parameter that can take a field name as the value. However, good...
Read more >
REST API Design: Filtering, Sorting, and Pagination
Filtering allows users to narrow down search results by defining specific criteria, while sorting allows results to be ordered in a particular ...
Read more >
REST API: Sorting, Filtering, and Pagination
Sorting allows you to order the results by any field, in ascending or descending order. Ascending vs. Descending. I always forget what ascending ......
Read more >
Pagination and Sorting using Spring Data JPA
In this tutorial, we'll learn how to easily paginate and sort using Spring Data JPA.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found