Appendium
- Overview
- Installation
- Basic Usage
- 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
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.1
Setup code base, It similar version 3.x.x
- 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.
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:
- JSON: {"Key":"website","Operate":"eq","Value":"www.mnlifeblog.com"}
- Example API: {{host}}/users?filters={"Key":"website","Operate":"eq","Value":"www.mnlifeblog.com"}
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
- JSON: {"or":[{"Key":"website","Operate":"eq","Value":"www.mnlifeblog.com"},{"Key":"name","Operate":"eq","Value":"mnlifeblog"}]}
- Example API: {{host}}/users?filters={"or":[{"Key":"website","Operate":"eq","Value":"www.mnlifeblog.com"},{"Key":"name","Operate":"eq","Value":"mnlifeblog"}]}
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 |
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.
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"}]
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
}
}
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