This is a built upon a complete idea my buddy Josh Newman came up with. We found it very hard to find any documentation or complete ideas for server-side pagination that handled nicely with any application layer. It’s really an easy concept.. we needed three things: total amount of records, page size, and the page number. We will leave the responsibility of handling the little bits of math for the application to send up to the request. The API should continue to be dumb and predictable.
To reduce the transactions on the database we used EntityFramework Extended to
Future()` our queries so they get called in one transactions rather than two. This is not required but will help with the load.
In order to return a set of results and the total amount of records for pagination we will need to return a
KeyValuePair<int, List<WhateverReturnModel>> from our data access methods.
Our API will return a result set like normal and a response header of
x-paging-total with the total amount of records so we can start paging!
#Step One: Create a pagination request model
#Step Two: Setup PagedResult return method
#Step Three: Setup the API endpoint return method
#Step Four: Write your method We’re using repository classes to handle our accessing of data
this.PageSkip is a helper method. You can replace this with:
(pageNumber - 1) * pageSize
#Step Five: Setup your API endpoint
That’s it. If you were to use Postman and hit that API endpoint you would get a result set of 15 records and a response header called
x-paging-total with the amount of records available to page.
Please tweet me if you have any questions! Good luck.