mirror of
https://github.com/Biarity/Sieve.git
synced 2024-11-25 06:43:34 +01:00
Update README.md
This commit is contained in:
parent
e848b86a16
commit
bcd7707e8f
22
README.md
22
README.md
@ -1,5 +1,5 @@
|
|||||||
# 🎛️ Sieve
|
# 🎛️ Sieve
|
||||||
Sieve is a simple and extensible framework for .NET Core that **adds sorting, filtering, and pagination functionality out of the box**.
|
Sieve is a simple, clean, and extensible framework for .NET Core that **adds sorting, filtering, and pagination functionality out of the box**.
|
||||||
Most common use case would be for serving ASP.NET Core GET queries.
|
Most common use case would be for serving ASP.NET Core GET queries.
|
||||||
|
|
||||||
## Usage for ASP.NET Core
|
## Usage for ASP.NET Core
|
||||||
@ -10,7 +10,7 @@ We'll use Sieve to add sorting, filtering, and pagination capabilities when GET-
|
|||||||
### 1. Add required services (`SieveProcessor<TEntity>`)
|
### 1. Add required services (`SieveProcessor<TEntity>`)
|
||||||
|
|
||||||
Inject the `SieveProcessor<TEntity>` service for each entity you'd like to use Sieve with.
|
Inject the `SieveProcessor<TEntity>` service for each entity you'd like to use Sieve with.
|
||||||
So to use Sieve with `Post`s, go to `ConfigureServices` in `Startup.cs` and add:
|
So to use Sieve with `Post`s, in `ConfigureServices` in `Startup.cs` add:
|
||||||
```
|
```
|
||||||
services.AddScoped<SieveProcessor<Post>>();
|
services.AddScoped<SieveProcessor<Post>>();
|
||||||
```
|
```
|
||||||
@ -18,7 +18,7 @@ services.AddScoped<SieveProcessor<Post>>();
|
|||||||
### 2. Add `Sieve` attributes on properties you'd like to sort/filter in your models
|
### 2. Add `Sieve` attributes on properties you'd like to sort/filter in your models
|
||||||
|
|
||||||
Sieve will only sort/filter properties that have the attribute `[Sieve(CanSort = true, CanFilter = true)]` on them (they don't have to be both true).
|
Sieve will only sort/filter properties that have the attribute `[Sieve(CanSort = true, CanFilter = true)]` on them (they don't have to be both true).
|
||||||
So for our `Post` entity model:
|
So for our `Post` entity model example:
|
||||||
```
|
```
|
||||||
public int Id { get; set; }
|
public int Id { get; set; }
|
||||||
|
|
||||||
@ -39,8 +39,8 @@ There is also the `name` parameter that you can use to have a different name for
|
|||||||
|
|
||||||
### 3. Use `SieveModel` in your controllers
|
### 3. Use `SieveModel` in your controllers
|
||||||
|
|
||||||
In the action handling returning Posts, use the `SieveModel` to get the sort/filter/paginate query.
|
In the action handling returning Posts, use `SieveModel` to get the sort/filter/paginate query.
|
||||||
Apply it by to your data by injecting `SieveProcessor<Post>` into the controller and using its `ApplyAll` method.
|
Apply it to your data by injecting `SieveProcessor<Post>` into the controller and using its `ApplyAll` method.
|
||||||
For instance:
|
For instance:
|
||||||
```
|
```
|
||||||
[HttpGet]
|
[HttpGet]
|
||||||
@ -70,7 +70,7 @@ Where `SieveCustomSortMethodsOfPosts` for example is:
|
|||||||
```
|
```
|
||||||
public class SieveCustomSortMethodsOfPosts : ISieveCustomSortMethods<Post>
|
public class SieveCustomSortMethodsOfPosts : ISieveCustomSortMethods<Post>
|
||||||
{
|
{
|
||||||
public IQueryable<Post> Popularity(IQueryable<Post> source, bool useThenBy, bool desc)
|
public IQueryable<Post> Popularity(IQueryable<Post> source, bool useThenBy, bool desc) // The method is given an indicator of weather to use ThenBy(), and if the query is descending
|
||||||
{
|
{
|
||||||
var result = useThenBy ?
|
var result = useThenBy ?
|
||||||
((IOrderedQueryable<Post>)source).ThenBy(p => p.LikeCount) :
|
((IOrderedQueryable<Post>)source).ThenBy(p => p.LikeCount) :
|
||||||
@ -78,7 +78,7 @@ public class SieveCustomSortMethodsOfPosts : ISieveCustomSortMethods<Post>
|
|||||||
.ThenBy(p => p.CommentCount)
|
.ThenBy(p => p.CommentCount)
|
||||||
.ThenBy(p => p.DateCreated);
|
.ThenBy(p => p.DateCreated);
|
||||||
|
|
||||||
return result;
|
return result; // Must return modified IQueryable<TEntity>
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
@ -86,12 +86,12 @@ And `SieveCustomFilterMethodsOfPosts`:
|
|||||||
```
|
```
|
||||||
public class SieveCustomFilterMethodsOfPosts : ISieveCustomFilterMethods<Post>
|
public class SieveCustomFilterMethodsOfPosts : ISieveCustomFilterMethods<Post>
|
||||||
{
|
{
|
||||||
public IQueryable<Post> IsNew(IQueryable<Post> source)
|
public IQueryable<Post> IsNew(IQueryable<Post> source, string op, string value) // The method is given the {Operator} & {Value}
|
||||||
{
|
{
|
||||||
var result = source.Where(p => p.LikeCount < 100 &&
|
var result = source.Where(p => p.LikeCount < 100 &&
|
||||||
p.CommentCount < 5);
|
p.CommentCount < 5);
|
||||||
|
|
||||||
return result;
|
return result; // Must return modified IQueryable<TEntity>
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
@ -101,12 +101,12 @@ Use the [ASP.NET Core options pattern](https://docs.microsoft.com/en-us/aspnet/c
|
|||||||
```
|
```
|
||||||
services.Configure<SieveOptions>(Configuration.GetSection("Sieve"));
|
services.Configure<SieveOptions>(Configuration.GetSection("Sieve"));
|
||||||
```
|
```
|
||||||
Then you can add configuration:
|
Then you can add the configuration:
|
||||||
```
|
```
|
||||||
{
|
{
|
||||||
"Sieve": {
|
"Sieve": {
|
||||||
"CaseSensitive": `boolean: should property names be case-sensitive? Defaults to false`,
|
"CaseSensitive": `boolean: should property names be case-sensitive? Defaults to false`,
|
||||||
"DefaultPageSize": `number: optional number to trim to when no page argument is given`
|
"DefaultPageSize": `number: optional number to fallback to when no page argument is given`
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
Loading…
Reference in New Issue
Block a user