This project is an attempt to mimic LimitOffsetPagination and CursorPagination that are available on DRF. Many other types of paginations can be incorporated beyond the ones available here. This is just a start.
dotnet add package DrfLikePaginations
It supports queries in your data given what is informed through the URL as query strings. You can get some details about how it works if you look at the tests in LimitOffsetPaginationITests.Queries class.
It also support model transformation. If you don't want to expose your model, you can create a DTO and then provide a function which transforms your data. Check out one example on this integration test.
The following project is using it and you can use as an example to set up yours:
Sample GIF that shows CursorPagination
:
Sample GIF that shows LimitOffsetPagination
:
You can add the following in your appsettings.json
:
{
"Pagination": {
"Size": 5
}
}
Then configure the pagination service like the following for LimitOffsetPagination
:
var paginationSize = int.Parse(Configuration["Pagination:Size"]);
services.AddSingleton<IPagination>(new LimitOffsetPagination(paginationSize));
You can use CursorPagination
also:
var paginationSize = int.Parse(Configuration["Pagination:Size"]);
// It will consider the field "id" to order by default, but you can change it ๐
services.AddSingleton<IPagination>(new CursorPagination(paginationSize));
Now you are able to use it ๐! One full example:
namespace EFCoreHandlingMigrations.Controllers.V1
{
[ApiController]
[Route("api/v1/[controller]")]
public class TodoItemsController : ControllerBase
{
private readonly DbSet<TodoItem> _databaseSet;
private readonly IPagination _pagination;
public TodoItemsController(AppDbContext context, IPagination pagination)
{
_databaseSet = context.TodoItems;
_pagination = pagination;
}
[HttpGet]
public async Task<Paginated<TodoItem>> GetTodoItems()
{
// You just need to apply OrderBy when using LimitOffsetPagination
var query = _databaseSet.AsNoTracking().OrderBy(t => t.CreatedAt);
var displayUrl = Request.GetDisplayUrl();
var queryParams = Request.Query;
return await _pagination.CreateAsync(query, displayUrl, queryParams);
}
}
}
If you look at docker-compose.yaml, you'll find three main services: