Building an ASP.NET Core Web API with Gridify

introduction

Data filtering, sorting, and pagination in modern web development are critical to building scalable and efficient APIs. Typically, implementing these features requires writing a lot of repetitive code for every endpoint. Enter Gridify, a powerful library that simplifies the filtering, sorting, and pagination process in ASP.NET Core Web APIs. In this article, we'll explore how to integrate Gridify into an ASP.NET Core Web API and leverage its features to build a more maintainable and efficient solution.

What is Gridify?

Gridify is a lightweight and flexible library designed to simplify common data operations like filtering, sorting, and pagination in ASP.NET Core applications. It can be integrated seamlessly into your existing projects, making data handling cleaner and more manageable.

Key Features

  • Dynamic Filtering: Filter data using LINQ queries.
  • Sorting: Sort data based on multiple properties.
  • Pagination: Easily implement pagination with minimal configuration.
  • Support for Complex Data Structures: Works with nested properties and custom mappings.

Getting Started with Gridify

Let’s dive into the implementation of Gridify in an ASP.NET Core Web API.

Step 1. Setting Up the Project

First, create a new ASP.NET Core Web API project using the .NET CLI.

dotnet new webapi -n GridifyDemo
cd GridifyDemo

Next, install the Gridify library.

dotnet add package Gridify

Step 2. Setting Up the Data Model

For this demo, we'll create a simple Product model.

public class Product
{
    public int Id { get; set; }
    public string Name { get; set; }
    public decimal Price { get; set; }
    public string Category { get; set; }
}

We’ll also set up an in-memory list of products to simulate a data source.

public static class ProductData
{
    public static List<Product> Products = new()
    {
        new Product { Id = 1, Name = "Laptop", Price = 800, Category = "Electronics" },
        new Product { Id = 2, Name = "Phone", Price = 600, Category = "Electronics" },
        new Product { Id = 3, Name = "Shoes", Price = 50, Category = "Fashion" },
        // Add more sample products here...
    };
}

Step 3. Integrating Gridify in the Controller

Create a controller named ProductsController and inject the Gridify functionality.

[Route("api/[controller]")]
[ApiController]
public class ProductsController : ControllerBase
{
    [HttpGet]
    public IActionResult Get([FromQuery] GridifyQuery gridifyQuery)
    {
        var products = ProductData.Products.AsQueryable();

        // Apply Gridify filtering, sorting, and pagination
        var result = products.ApplyGridify(gridifyQuery);

        return Ok(result);
    }
}

Step 4. Testing Gridify in Action

Gridify enables querying using straightforward syntax. Let’s break down how it works.

Filtering

You can filter data using query strings like,

GET /api/products?filter=Category=Electronics

This query will return only products in the "Electronics" category.

Sorting

Sorting is also straightforward.

GET /api/products? Sort=Price

You can even sort in descending order.

GET /api/products? Sort=-Price

Pagination

Pagination parameters include Page and PageSize.

GET /api/products? Page=1&pageSize=2

This query will return the first two products.

Custom Mappings with Gridify

Sometimes you may need custom mappings for more complex scenarios. Gridify allows you to define mappings easily.

GridifyGlobalConfiguration.DefaultEntityMapping<Product>()
    .Map("cheapProducts", p => p.Price < 100);

With this custom mapping, you can now filter products based on your custom condition.

GET /api/products? Filter=cheapProducts

Conclusion

Gridify is a highly flexible and powerful library that can save you a lot of time when implementing filtering, sorting, and pagination in your ASP.NET Core Web APIs. By abstracting away the repetitive code typically associated with these operations, Gridify allows you to focus more on building robust features and less on boilerplate code.