Giter Site home page Giter Site logo

mikessifter's Introduction

MikesSifter

MikesSifter is a versatile and extensible library designed to provide powerful filtering, sorting, and paging capabilities in .NET applications.

Installation

Minimum Requirements: .NET 8.0.x

Download from NuGet

PowerShell
NuGet\Install-Package MikesSifter -Version *version_number*
Cmd
dotnet add package MikesSifter --version *version_number*

Usage for ASP.NET Core WebApi

In this example, consider an app with a User entity that can have many projects. We'll use MikesSifter to add sorting, filtering, and pagination capabilities when retrieving all available users.

1. Configure the properties you want to sort/filter in your models.

1.1. Using a modular entity configuration class.

public class UserSifterConfiguration : IMikesSifterEntityConfiguration<User>
{
    public void Configure(MikesSifterEntityBuilder<User> builder)
    {
        builder
            .Property(e => e.FullName)
            .EnableFiltering()
            .EnableSorting();

        builder
            .Property(e => e.Gender)
            .EnableSorting()
            .EnableFiltering();

        builder
            .Property(e => e.BirthDate)
            .EnableFiltering()
            .EnableSorting();

        builder
            .Property(e => e.Projects)
            .EnableFiltering()
            .HasCustomFilter(FilteringOperators.Contains, filterValue =>
            {
                ArgumentException.ThrowIfNullOrWhiteSpace(filterValue);
                return u => u.Projects.Any(e => e.Id == Guid.Parse(filterValue));
            });

        builder
            .Property(e => e.Passport.Number)
            .EnableFiltering()
            .EnableSorting()
            .HasAlias("user_passportNumber");
    }
}

Apply configurations by calling ApplyConfiguration<T>:

builder.ApplyConfiguration<UserSifterConfiguration>();

Apply configurations from a particular assembly by calling ApplyConfigurationsFromAssembly:

builder.ApplyConfigurationsFromAssembly(Assembly.GetExecutingAssembly());

1.2. Using Fluent API without defining a separate configuration class.

builder.Entity<User>(e =>
{
    e.Property(i => i.FullName)
        .EnableFiltering()
        .EnableSorting();

    e.Property(i => i.Gender)
        .EnableSorting()
        .EnableFiltering();

    e.Property(i => i.BirthDate)
        .EnableFiltering()
        .EnableSorting();

    e.Property(i => i.Projects)
        .EnableFiltering()
        .HasCustomFilter(FilteringOperators.Contains, filterValue =>
        {
            ArgumentException.ThrowIfNullOrWhiteSpace(filterValue);
            return u => u.Projects.Any(pr => pr.Id == Guid.Parse(filterValue));
        });

    e.Property(i => i.Passport.Number)
        .EnableFiltering()
        .EnableSorting()
        .HasAlias("user_passportNumber");
});

2. Implement IMikesSifterModel.

In our example, we will use a custom model implementation as the POST body. However, you can implement your own using, for example, GET query parameters.

public sealed class ApplicationSifterModel : IMikesSifterModel
{
    public FilteringOptions? FilteringOptions { get; init; }
    
    public SortingOptions? SortingOptions { get; init; }
    
    public PagingOptions? PagingOptions { get; init; }

    public FilteringOptions? GetFilteringOptions() => FilteringOptions;

    public SortingOptions? GetSortingOptions() => SortingOptions;

    public PagingOptions? GetPagingOptions() => PagingOptions;
}

3. Define the application sifter.

Inherit the base implementation MikesSifter and override the Configure method.

public class ApplicationSifter : MikesSifter
{
    protected override void Configure(MikesSifterBuilder builder)
    {
        builder.ApplyConfiguration<UserSifterConfiguration>();
    }
}

4. Dependency Injection (DI).

Add the application sifter to the services by calling the AddSifter extension method.

builder.Services.AddSifter<ApplicationSifter>();

5. Let's use.

Inject IMikesSifter into controller to use the sifter capabilities.

5.1. Apply filtering/sorting/paging by calling the Apply method.

[HttpPost("full")]
public IActionResult Full(ApplicationSifterModel model)
{
    var result = sifter.Apply(dbContext
        .Users
        .Include(e => e.Projects)
        .Include(e => e.Passport), model);
    
    return Ok(result.Select(e => e.ToViewModel()).ToList());
}

5.2. Apply only filtering by calling the ApplyFiltering method.

[HttpPost("filtering")]
public IActionResult OnlyFiltering(FilteringOptions filteringOptions)
{
    var result = sifter.ApplyFiltering(dbContext
        .Users
        .Include(e => e.Projects)
        .Include(e => e.Passport), filteringOptions);
    
    return Ok(result.Select(e => e.ToViewModel()).ToList());
}

5.3. Apply only sorting by calling the ApplySorting method.

[HttpPost("sorting")]
public IActionResult OnlySorting(SortingOptions sortingOptions)
{
    var result = sifter.ApplySorting(dbContext
        .Users
        .Include(e => e.Projects)
        .Include(e => e.Passport), sortingOptions);
    
    return Ok(result.Select(e => e.ToViewModel()).ToList());
}

5.4. Apply only paging by calling the ApplyPaging method.

[HttpPost("paging")]
public IActionResult OnlyPaging(PagingOptions pagingOptions)
{
    var result = sifter.ApplyPaging(dbContext
        .Users
        .Include(e => e.Projects)
        .Include(e => e.Passport), pagingOptions);
    
    return Ok(result.Select(e => e.ToViewModel()).ToList());
}

mikessifter's People

Contributors

vazovsk1y avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.