Skip to content

Tired of creating pagination all the time in your .NET projects? Apply either Offset or Cursor pagination and focus on business logic ๐Ÿ™‚

License

Notifications You must be signed in to change notification settings

willianantunes/drf-like-paginations

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

47 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

DRF Like Paginations

Coverage Lines of Code

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:

See it in action!

Sample GIF that shows CursorPagination:

Sample usage of how CursorPagination works

Sample GIF that shows LimitOffsetPagination:

Sample usage of how LimitOffsetPagination works

How to use it

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);
        }
    }
}

Compose services

If you look at docker-compose.yaml, you'll find three main services:

  • tests: run all the tests, and generate tests-reports folder with coverage data.
  • lint: check if the project is valid given standard dotnet-format rules
  • formatter: format the project given standard dotnet-format rules

About

Tired of creating pagination all the time in your .NET projects? Apply either Offset or Cursor pagination and focus on business logic ๐Ÿ™‚

Topics

Resources

License

Stars

Watchers

Forks