Infrastructure.Service

You can query entity by dynamic criteria build on Front-end. Allow sorts, paging and restriction fields.


Keywords
.NET, .NetCore, ASP.NET, CSharp, DynamicSearch, EFCore, LINQ
License
MIT
Install
Install-Package Infrastructure.Service -Version 4.0.1

Documentation

Infrastructure Service

Appendium

  • Overview
  • Installation
  • Basic Usage

Overview

  • Open end-point API allow build dynamic query for searching and paging. It auto compile query from text on Front-end to Expression Tree Linq.
  • Integration with Infrastructure.Repository
  • Support .NetCore if you want run on .NetFramework you need override the Repository Layer

Installation

To install Infrastructure Service Infrastructure.Service library, right-click on your project in Solution Explore,then select Manage NuGet Packages..., and install the following package.

  • Infrastructure.Service

You can also install this library using .NET CLI

dotnet add package Infrastructure.Service --version 4.0.0

Setup code base, It similar version 3.x.x

  • Guide for setup code

Basic Usage

  • End-point API will be public with append some query params: filters, sorts, pageSize, pageIndex. You can view more BaseCriteria's class
  • You should take care some class:
    • BaseCriteria: the query params for api
    • PagedList: the response model
    • ISearchService: the service handler logic.

Simple Query

Single query

When querying data using Linq, everything can be express look like below

var result = context.Users.Where(s => s.Website == "www.mnlifeblog.com");

Infrastructure.Service look like:

Multiple query

Note that it's also possible to query data using multiple predicates:

var result = context.Users.Where(s => s.Website == "www.mnlifeblog.com" ||
                                      s.Name == "mnlifeblog");

That code can be used when using Infrastructure.Service

Operand summary table

Operate Translate Description
eq == Equals
neq != Not Equals
gt > Greater Than
gte >= Greater Than Equals
lt < Less Than
lte <= Less Than Equals
in Contains Contains
nin !Contains Not Contains
btw >= <= Between

Sorts

Sort by ASC or DESC

When use orderby on Linq:

 var result = context.Users.Orderby(s => s.Website);

The code can be use on Infrastructure.Service:

  • JSON: {"key":"website","criteria":"asc"}
  • Example API: {{host}}/users?sorts={"key":"website","criteria":"asc"}

When you want to apply DESC for sort you just alter asc to desc.

Multiple sort

The strongly typed LINQ:

 var result = context.Users.Orderby(s => s.Website).ThenByDescending(s => s.Date);

The code you can build:

  • JSON: [{"key":"website","criteria":"asc"},{"key":"date","criteria":"desc"}]
  • Example API: {{host}}/users?sorts=[{"key":"website","criteria":"asc"},{"key":"date","criteria":"desc"}]

Paging

Depend on pageIndex and pageSize field for paging

  • Example API: {{host}}/users?pageIndex=1&&pageSize=20

The payload response:

{
  "paged": {
    "totalCount": 42,
    "count": 10,
    "pageIndex": 1,
    "pageSize": 10,
    "durationMilliseconds": 10,
    "queryMilliseconds": 8,
    "totalMiliseconds": 10
  }
}

Validation

You can restrict some fields and not allow search by SearchRestrictions's section on appsetting.json

  "SearchRestrictions": {
    "User": "name, id, date ",
  }

Take note: the fields separated by a comma and it not case sensitive

The user's property on JSON will be matched to the name of the entity.

 public class User : IEntity<Guid>
    {
        public Guid Id { get; set; }
        public string Name { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string Email { get; set; }
        ...
    }

Throw exception message: "The field is restricted!" when you access to field restricted.

Source: https://github.com/KhaMinhVLU-2017/Infrastructure.Service

Thanks for watching