Giter Club home page Giter Club logo

gridify's Introduction

Gridify

GitHub Nuget

Easy and optimized way to apply Filtering, Sorting and pagination using text-based data.

The best use case of this library is Asp-net APIs. when you need to get some string base filtering conditions to filter data or sort it by a field name or apply pagination concepts to your lists and return a pageable, data grid ready information, from any repository or database.


WebApi Simple Usage example

// ApiController

[Produces(typeof(Paging<Person>))]
public IActionResult GetPersons([FromQuery] GridifyQuery gQuery)
{
    // Gridify => Filter,Sort & Apply Paging
    // in short, Gridify returns data especially for data Grids.
    return myDbContext.Persons.Gridify(gQuery);
}

complete request sample:

http://exampleDomain.com/api/GetPersons?pageSize=100&page=1&sortBy=FirstName&isSortAsc=false&filter=Age%3D%3D10

also we can totally ignore GridifyQuery

http://exampleDomain.com/api/GetPersons

What is GridifyQuery (basic usage example)

GridifyQuery is a simple class for configuring Filtering,Paging,Sorting.

// usually, we don't need to create this object manually
// for example, we get this object as a parameter from our API Controller
var gQuery = new GridifyQuery()
{
    Filter = "FirstName==John",
    IsSortAsc = false,
    Page = 1,
    PageSize = 20,
    SortBy = "LastName"
};

Paging<Person> pData =
         myDbContext.Persons  // we can use Any list or repository or EntityFramework context
          .Gridify(gQuery); // Filter,Sort & Apply Paging


// pData.TotalItems => Count persons with 'John', First name
// pData.Items      => First 20 Persons with 'John', First Name

Installation

Install the Gridify NuGet Package.

Package Manager Console

Install-Package Gridify

.NET Core CLI

dotnet add package Gridify

Extentions

The library adds below extension methods to IQueryable:

Extension Description
ApplyFiltering Apply Filtering using string Filter property of GridifyQuery class and returns an IQueryable<T>
ApplyOrdering Apply Ordering using string SortBy and bool IsSortAsc properties of GridifyQuery class and returns an IQueryable<T>
ApplyPaging Apply paging using short Page and int PageSize properties of GridifyQuery class and returns an IQueryable<T>
ApplyOrderingAndPaging Apply Both Ordering and paging and returns an IQueryable<T>
ApplyEverything Apply Filtering,Ordering and paging and returns an IQueryable<T>
GridifyQueryable Like ApplyEverything but it returns a QueryablePaging<T> that have an extra int totalItems property to use for pagination
Gridify Receives a GridifyQuery ,Load All requested data and returns Paging<T>

TIP:

Gridify function is an ALL-IN-ONE package, that applies filtering and ordering and paging to your data and returns a Paging<T>,

but for example, if you need to just filter your data without paging or sorting options you can use ApplyFiltering function instead.


Supported Filtering Operators

Name Operator Usage example
Equal == "FieldName==Value"
NotEqual != "FieldName!=Value"
GreaterThan >> "FieldName>>Value"
LessThan << "FieldName<<Value"
GreaterThanOrEqual >= "FieldName>=Value"
LessThanOrEqual <= "FieldName<=Value"
Contains - Like =* "FieldName=*Value"
NotContains - NotLike !* "FieldName!*Value"
AND - && , "FirstName==Value,LastName==Value2"
OR - || | "FirstName==Value|LastName==Value2"
Parenthesis () "(FirstName=*Jo,Age<<30)|(FirstName!=Hn,Age>>30)"

we can easily create complex queries using Parenthesis() with AND (,) + OR (|) operators.


Custom Mapping Support

By default Gridify is using a GridifyMapper object that automatically maps your string based field names to actual properties in your Entities but if you have a custom DTO (Data Transfer Object) you can create a custom instance of GridifyMapper and use it to create your mappings.

// example Entities
public class Person
{
    public string FirstName {get;set;}
    public string LastName {get;set;}
    public Contact Contact {get;set;}

}
public class Contact
{
    public string Address {get;set;}
    public int PhoneNumber {get;set;}
}

// example DTO
public class PersonDTO
{
   public string FirstName {get;set;}
   public string LastName {get;set;}

   public string Address {get;set;}
   public int PhoneNumber {get;set;}
}

//// GridifyMapper Usage example -------------

var customMappings = new GridifyMapper<Person>()
        // because FirstName and LastName is exists in both DTO and Entity classes we can Generate them
        .GenerateMappings()
        // add custom mappings
        .AddMap("address", q => q.Contact.Address )
        .AddMap("PhoneNumber", q => q.Contact.PhoneNumber );


// as i mentioned before. usually we don't need create this object manually.
var gQuery = new GridifyQuery()
{
    Filter = "FirstName==John,Address=*st",
    IsSortAsc = true,
    SortBy = "PhoneNumber"
};

// myRepository: could be entity framework context or any other collections
var gridifiedData = myRepository.Persons.Gridify(gQuery, customMappings);

by default GridifyMapper is Case-insensitive but you can change this behavior if you need Case-Sensitive mappings.

var customMappings = new GridifyMapper<Person>(true); // mapper is case-sensitive now.

Combine Gridify with AutoMapper

//AutoMapper ProjectTo + Filtering Only, example
var query = myDbContext.Persons.ApplyFiltering(gridifyQuery);
var result = query.ProjectTo<PersonDTO>().ToList();

// AutoMapper ProjectTo + Filtering + Ordering + Paging, example
QueryablePaging<Person> qp = myDbContext.Persons.GridifyQueryable(gridifyQuery);
var result = new Paging<Person> () { Items = qp.Query.ProjectTo<PersonDTO>().ToList (), TotalItems = qp.TotalItems };

EntityFramework integration

if you need to use gridify async feature for entityFramework Core, use Gridify.EntityFramework package instead.

this package have two additional GridifyAsync() and GridifyQueryableAsync() functions.

dotnet add package Gridify.EntityFramework
or
dotnet add package Gridify.EntityFramework5

Contribution

Any Contribution to improve documentation and library is appreciated feel free to send pull-Request. <3

gridify's People

Contributors

alirezanet avatar ariadarkkkis avatar dev1no avatar

Stargazers

 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.