Master the Art of Pagination in .NET 6.0 with YannikG.PageableData: An Easy Guide

Yannik Gartmann
5 min readFeb 4, 2023
Photo by Anastasia Zhenina on Unsplash

Introduction to Pagination

Pagination is widely used in web applications, APIs and websites to handle the large amount of data efficiently. By breaking down the data into smaller chunks, it provides a better user experience and improved performance. In APIs, pagination is often used to limit the number of records returned in a single request, helping to reduce the response time and the amount of data transferred over the network.

On websites, pagination is used to navigate through pages of content, such as product listings, blog posts, or search results. It also helps to reduce the amount of data displayed on a single page, making it easier for the user to view and navigate through the information.

However, implementing pagination in .NET can be a challenging task, especially for developers who are new to the language.

The Need for YannikG.PageableData

To simplify the process of pagination in .NET, it is essential to have a toolkit that takes care of the annoying calculations and provides an easy-to-use interface. This is where YannikG.PageableData comes in. It is a powerful nuget package that makes it effortless to paginate large data sets in .NET for any data source.

Installing YannikG.PageableData Nuget Package

Installing YannikG.PageableData is quick and straightforward. Simply follow these steps:

  1. Open your .NET Core project in Visual Studio
  2. Go to the Package Manager
  3. Type “YannikG.PageableData” and press enter
  4. Select the package and hit install
  5. The package will be installed and added to your project dependencies

Or simply run dotnet add package YannikG.PageableData within your project.

Hwo to use YannikG.PageableData

Getting to know the functionalities

The IPageable interfaces offer a comprehensive toolkit of useful and standardized properties for your API requests. When making a request, you can specify the desired page and its size (maximum results), and even sort the data by sending a field name and sorting direction. The sorting is handled in the data access logic, while the package itself has no impact on your database queries. It simply provides an easy-to-use toolkit.

Sample Application WeatherForecastController parameters

IPageable also provides a convenient way to pre-calculate the Skip and Take values. This eliminates the need for manual calculations, streamlining your data querying process and making it more efficient.

Sample Application WeatherForecastRepository Skip and Take

To ensure successful data transfer back to the requester, the use of the DataPage class is highly recommended. This class functions as a container, holding both the IPageable information from the request and the queried data, facilitating smooth and organized data transfer.

Sample Application WeatherForecastRepository return

Example Walkthrough

With this example walkthrough, I’m illustrating how to send a GET Request to a weather forecast endpoint in order to retrieve the second page (with page numbers starting at 0) of weather data. The request specifies a PageSize of 10, with the desired CurrentPage being 1. The SortByField parameter is set to “City”, and the SortDirection is set to 1 for ascending order (a value of -1 would indicate descending order).

https://example.com/WeatherForecast?PageSize=10&CurrentPage=1&SortByField=City&SortDirection=1

Upon successful execution of the request, the endpoint returns a JSON object that includes valuable information about the retrieved data:

{
"totalItems": 34,
"totalItemsOnPage": 10,
"content": [
{
"city": "Frauenfeld",
"date": "2022-06-15T20:52:26.599853+02:00",
"temperatureC": 32,
"temperatureF": 89,
"summary": "Scorching"
},
...
{
"city": "Lugano",
"date": "2022-06-15T20:52:26.599857+02:00",
"temperatureC": 44,
"temperatureF": 111,
"summary": "Sweltering"
}
],
"totalPages": 4,
"pageSize": 10,
"currentPage": 1,
"isSorted": true,
"sortByField": "City",
"sortDirection": 1
}

The totalItems property shows the total number of items available in the database, while the totalItemsOnPage property shows the number of items returned on the current page. The content property holds an array of weather forecasts.

The response also includes other useful information, such as the total number of pages (totalPages), the page size (pageSize), the current page (currentPage), and details about the sort, such as whether the data is sorted (isSorted), the sort by field (sortByField), and the sort direction (sortDirection).

In conclusion, this example shows the simplicity and efficiency of querying data with pagination, making it an essential aspect in managing large data sets in any web application.

Background to this package

As a software developer who has worked with Java Spring Boot, I found myself missing the convenient paging features Spring Data offered. This led me to create my own solution, resulting in a lightweight and easy-to-extend Nuget package. This package offers the ease-of-use that I had grown attached to with Java Spring Boot, making data pagination a much more manageable task in .NET.

I have successfully integrated this package into my own projects, streamlining the process of managing and navigating large data sets. With its user-friendly design and helpful features, this package has proven to be a valuable asset in my development work.

Read more

Link to the repository: https://github.com/YannikG/dotnet-pageable-data

Conclusion

In conclusion, YannikG.PageableData is a powerful and easy-to-use library that makes it simple to implement pagination in .NET. With its fast performance, customizable options, and lightweight design, it is an excellent solution for managing large data sets in your web applications. So, if you’re looking to simplify the process of pagination in .NET, give YannikG.PageableData a try and discover its benefits for yourself.

Contribute

I hope you found YannikG.PageableData useful in simplifying the process of pagination in .NET. As an open-source project, I welcome contributions from the community to make it even better. Whether it’s fixing bugs, adding new features, or improving documentation, every little bit helps.

Happy coding!

--

--

Yannik Gartmann

DevOps Engineer, Photographer, and Railway enthusiast.