How do you handle filtering/sorting collections returned on endpoints?
I have just started making APIs and one of the most common tasks is filtering and sorting returned collections based on query params/route. After making a few endpoints that return collections that can be sorted in a grid I realised that I am going to need some generic methods so I don't have to continually write out switches and if/else blocks to handle it.
I came up with a system using an interface that looks for certain properties which feed filters and sort options into dynamic linq query on the entity repository. It did the job for the project but it's very opinionated and now I am coming to my next project I can see that I really should have something robust enough that I can drop into any project. I mage a dynamic PaginatedList<T> class that takes an IQueryable which works great for doing pagination with a little meta data but I am struggling to see the best way to also implement generic sorting and filtering that can handle different types of filters on different columns without a lot of code that probably already exists somewhere.
My google-fu has let me down and I haven't really found any libraries or suitable code examples but this feels like such a common task that I know there must be a few projects out there where someone has already gone through all the iterations to find something that works well in most situations. I am probably not using the right terminology in my searches.
Any suggestions?
0 comments:
Post a Comment