Adding Filtering, Sorting And Pagination To a REST API | .NET 7

preview_player
Показать описание

REST APIs are the industry standard when it comes to building APIs. There are many nuances to building a good API, and it becomes more complicated in a Clean Architecture & DDD-styled system. I showed you in a previous video how to make a set of CRUD endpoints and some of the REST API best practices you should consider. And now, we're continuing our REST API journey by adding support for filtering, sorting, and pagination.

Join my weekly .NET newsletter:

Read my Blog here:

Subscribe for more:

Chapters
0:00 Adding an endpoint for getting all records
3:22 Implementing Filtering in the API
6:06 Supporting LINQ Contains with ValueObjects
8:18 Introducing Sorting in the API
14:42 Adding Pagination support in the API
17:41 Wrapping the response in a PagedList
Рекомендации по теме
Комментарии
Автор

I've been looking for a channel like yours for a long time and now I found it. It's your channel Milan and I'm so happy. Thank you very much for your knowledge sharing. I still have to find a good mentor on the front end like you are for me on the back end 🙏🙏🙏

kodindoyannick
Автор

On 13:19, you could create an IQueryable<T> extension method that takes the sort order field and your expression and does the decision for OrderBy or OrderByDescending behind the scenes. Removes a bit of boilerplate code that you don't need to copy paste for each search endpoint in your API.

CreativeBST
Автор

Where should I put the PagedList<T> model? In the domain layer or in the application?

swift
Автор

Have you already done a full tutorial on your channel? I like your teaching style! I wish there is a full tutorial or even a course

adeni
Автор

Waiting for the Dapper version too! Great video.

gusflopes
Автор

Thanks for this awesome video.I was searching from long time 😊.

Karthik-ugll
Автор

A few years ago I used a library for parsing OData query options called Moon.OData. I could send an OData query and the program translated it into a normal t-sql. Very simple and safe for both user and developer.
I believe there are more such libraries nowadays. Would definitely recommend that to anyone.

daveanderson
Автор

Milan, this was a very useful video tutorial. thx a lot

christianguitarcovers
Автор

Your videos are SO good! Thank you for teaching me so much!

EHBRod
Автор

I had to create something similar but the backend DB was Mongo so I had to use projection, and for the search I used reflection to get the Data Annotation for the BsonElement name and type, the documents had over 150 fields ... It would be nice to add reflection to this tutorial to avoid hard coding the name of the properties, maybe using JsonProperty annotation??? Thanks for the great tutorial

rafaelg
Автор

I was expecting something more generic like Sieve for automatic pagination sorting and filtering.
Or OData or graph

Rodrick.
Автор

Excellent video! Really helpful! Thanks for sharing! by the way, can you explain in a short video format the record and sealed modifiers from your perspective and it's importance the documentation not consider optimization benefits. Thanks

ccuenca
Автор

I highly suggest you determine a maximum page size and use that instead of what the client is requesting if their pagesize exceeds your maximum. :)

Also, I think I like returning the pagination information in headers, rather than in the response payload.

pilotboba
Автор

Thanks for sharing this insightful, in all my api projects i use rye same logic for paging, filtering and sorting. For the pagination i return an object that contains items page number and page size as you did, however instead of returning a boolean HasNextPage or HasPreviousPage, i return string which are url for the previous and next pages already prepared (when i'm one the first page, previous page is null and if i'm in the last page next page is gonna be null )so in case one of clients of my api is working on a batch for frtching data url are already prepared by the api 😃

Thanks for sharing i really like your helpful content

zakariasmahi
Автор

I loved the Expression with switch to cover the which column to sort by, in my project I have the same amount of search choices I want to cover, so I'm using if else statements, can you please show case or at least right here as a respond an Expression with switch that is used in Where() instead of OrderBy(). I tried it but I'm messing something and i can't get it to work. Thanks a lot for the video, it helped a lot

jamalalbatta
Автор

Thank you for covering those topics which are helpful in handling performance issues.
On filtering, if the table has multiple columns, then the number of parameters may grow. is there a generic way to easily achieve it.
On pagination, if the result fits in 5 pages, the client should call the API for 5 times. So, the end query will be executed 5 times by considering the page number. will it not affect the sql performance

gakshay
Автор

It seems like a good tutorial but I am having difficulty following your architecture and stuff about record and sku. Are there any prior videos that I need to watch? This has been happening to me for quite a few videos so any help in that will be very appreciated.

samuniv
Автор

Why you didn’t use graphql with EF Core ?

Pawelogll
Автор

This is good stuff. EF is fine when returning simple entities from the DB. At 13:46 I would have taken the refactoring a little further and eliminate the "8-liner" (as I like to to call those if..else pairs that simply assign a variable) with the ternary operator.

I'm not a fan the PageList stuff, returning the TotalCount is a very expensive choice with, IMO, limited value. Whether there is a previous or next page is of even less value. (E.g. I know I just got page 1 so I don't really need the server to tell me there isn't a previous page and for reasonable page sizes (say 20) 95% of the time I can tell there is no next page because the count of results returned is < pageSize). I prefer the simplicity of just returning the set of results but I guess it is down to your specific use cases.

codingbloke
Автор

Does the select hurt performance of the take, skip, or count async? Calling ToListAsync is when the queryable is actually executed but how does it know to reorder the operations in a performant way?

crazyfox