asp.net core 排序过滤分页组件:sieve

Posted pangjianxin

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了asp.net core 排序过滤分页组件:sieve相关的知识,希望对你有一定的参考价值。

使用asp.net core开发时避免不了要用一个合适的分页组件来让前端获取分页数据。github上面有一个开源的分页组件在这方面很适合我的使用,于是我把他的文档翻译一下,随后会分析它里面的源码。这是第一篇,介绍它如何使用。

Sieve

Sieves是一个.net core下面的简单、干净并且可扩展的框架,它对外暴露了排序,过滤和分页的功能。

ASP.NET Core下的使用方式

在本例中,考虑一个带有Post实体的应用程序。在获取所有可用的帖子时,我们将使用Sieve添加排序、过滤和分页功能。

1. 添加必要的服务

在StartUp.cs文件中编辑ConfigureService方法,将SieveProcessor服务注入进去:

 

services.AddScoped<SieveProcessor>();

2. 然后告诉Sieve你要让哪几个属性可以进行排序或过滤

Sieve只会对标记有Sieve Attribute的属性(例如:[Sieve(Cansort=true,CanFilter=true)])进行排序或者过滤,所以,在我们的Post的例子中:

技术图片
public int Id { get; set; }

[Sieve(CanFilter = true, CanSort = true)]
public string Title { get; set; }

[Sieve(CanFilter = true, CanSort = true)]
public int LikeCount { get; set; }

[Sieve(CanFilter = true, CanSort = true)]
public int CommentCount { get; set; }

[Sieve(CanFilter = true, CanSort = true, Name = "created")]
public DateTimeOffset DateCreated { get; set; } = DateTimeOffset.UtcNow;
View Code

Name参数可以为客户端提供一个不一样的名字

NOTE:你可以使用Fluent API实现一样的功能,就像EF Core中的Fluent Api(IEntityTypeConfiguration)一样的道理和用法。

3.在Controller中使用SieveModel来获取排序、过滤以及分页查询参数

在下面这个Action中处理返回的Post,使用SieveModel来获取排序过滤以及分页查询参数。同时,将SieveProcessor注入到你的Controller中,只要使用它的Apply<TEntity>方法就可以了:

技术图片
[HttpGet]
public JsonResult GetPosts(SieveModel sieveModel) 
{
    var result = _dbContext.Posts.AsNoTracking(); // Makes read-only queries faster
    result = _sieveProcessor.Apply(sieveModel, result); // Returns `result` after applying the sort/filter/page query in `SieveModel` to it
    return Json(result.ToList());
}
View Code

你也可以通过应用这个方法中的可选参数来显式的指定你的意图,比如你只想要过滤或者配许,或者分页。具体查看它的Api吧。

4.发送一个请求

GET /GetPosts

?sorts=     LikeCount,CommentCount,-created         // sort by likes, then comments, then descendingly by date created 
&filters=   LikeCount>10, [email protected]=awesome title,     // filter to posts with more than 10 likes, and a title that contains the phrase "awesome title"
&page=      1                                       // get the first page...
&pageSize=  10                                      // ...which contains 10 posts

添加自定义的排序/过滤方法

如果你想要添加自定义的排序和过滤方法,注入ISieveCustomSortMethodsISieveCustomFilterMethods这两个接口的实现就可以了,Sieve会查询到你注入的这两个接口并应用他们。

举例来说:

技术图片
services.AddScoped<ISieveCustomSortMethods, SieveCustomSortMethods>();
services.AddScoped<ISieveCustomFilterMethods, SieveCustomFilterMethods>();
View Code

SieveCustomSortMethodsOfPosts是下面这样的:

技术图片
public class SieveCustomSortMethods : ISieveCustomSortMethods
{
    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 ?
            ((IOrderedQueryable<Post>)source).ThenBy(p => p.LikeCount) : // ThenBy only works on IOrderedQueryable<TEntity>
            source.OrderBy(p => p.LikeCount)
            .ThenBy(p => p.CommentCount)
            .ThenBy(p => p.DateCreated);

        return result; // Must return modified IQueryable<TEntity>
    }
}
View Code

SieveCustomFilterMethods

技术图片
public class SieveCustomFilterMethods : ISieveCustomFilterMethods
{
    public IQueryable<Post> IsNew(IQueryable<Post> source, string op, string[] values) // The method is given the {Operator} & {Value}
    {
        var result = source.Where(p => p.LikeCount < 100 &&
                                        p.CommentCount < 5);

        return result; // Must return modified IQueryable<TEntity>
    }
}
View Code

配置Sieve

使用ASP.NET Core options pattern这种方式来配置SieveOption,告诉Sieve从哪里找到配置。比如:

services.Configure<SieveOptions>(Configuration.GetSection("Sieve"));

你可以在Json文件中添加配置:

技术图片
{
    "Sieve": {
        "CaseSensitive": "boolean: should property names be case-sensitive? Defaults to false",
        "DefaultPageSize": "int number: optional number to fallback to when no page argument is given. Set <=0 to disable paging if no pageSize is specified (default).",
        "MaxPageSize": "int number: maximum allowed page size. Set <=0 to make infinite (default)",
        "ThrowExceptions": "boolean: should Sieve throw exceptions instead of silently failing? Defaults to false"
    }
}
View Code

发送一个请求

上面那些步骤你都做了的话,你可以发送一个GET请求,请求中携带排序、过滤和分页查询参数,下面一个例子:
GET /GetPosts

?sorts=     LikeCount,CommentCount,-created         // sort by likes, then comments, then descendingly by date created 
&filters=   LikeCount>10, [email protected]=awesome title,     // filter to posts with more than 10 likes, and a title that contains the phrase "awesome title"
&page=      1                                       // get the first page...
&pageSize=  10                                      // ...which contains 10 posts

更多的信息:
  • sorts是按逗号分隔的有序属性名列表。在名称之前添加一个“-”将切换到递减排序。
  • filters是一组以逗号分隔的{名称①}{操作符②}{值③},下面对大括号中的这三个部分做解释:①名称,是你自定义的过滤方法中的方法名称或者SieveAttribute中的Name属性的值。你还可以通过将多个名称(作为OR的逻辑)括在括号中,并使用管道分隔符,例如。(LikeCount|CommentCount)>10询问LikeCount或CommentCount是否为>10。②操作符是下面要介绍的操作符中的一种(下面会介绍)。③值是会被过滤功能所采用的值。你同样可以使用多个值(作为OR逻辑),比如[email protected]=new|hot会返回Title包含“new”或者“hot"的Post。
  • page是要返回的分页数据的页数。
  • pageSize是每一页所包含的条目数。

注意:

  • 可以使用反斜杠转义值字段中的逗号和管道
  • 可以在除{名称}或{操作符}字段之外的任何地方使用空格
  • 如果需要在应用分页之前查看数据(例如:获取总计数),使用Apply上的可选参数来延迟分页,这里有一个例子:
//代码直接摘抄自这个项目的github上面的issure中,你可以自己打开看
From my own experience Id like the ability to get the total count while filtering and paginating. When paginating result.Count() will be the number of records on that page, not the total number of records. This alone doesnt give enough information to, say, power a pagination component as we dont know the number of total pages/records. Though, if Im wrong Id be pretty happy as Im currently doing this as a workaround: public async Task<IActionResult> GetQuestions(SieveModel sieveModel) { var questions = _context.Questions.AsNoTracking(); questions = _sieveProcessor.Apply(sieveModel, questions); var total = _context.Questions.AsNoTracking(); total = _sieveProcessor.Apply(sieveModel, total, applySorting: false, applyPagination: false); Request.HttpContext.Response.Headers.Add("X-Total-Count", total.Count().ToString()); return Ok(questions); }
  • 这里有一个如何在IEnumerable上面工作的例子:
Youll have to write a custom filter for that. Heres an example for v>=1.3.7:

public class SieveCustomFilterMethods : ISieveCustomFilterMethods
{
    public IQueryable<Post> AuthorIsAnyOf(IQueryable<Post> source, string op, string value)
    {
        var result = source.Where(p => value.Split(|).Contains(p.Author));
        return result;
    }
}

Then inject the class: services.AddScoped<ISieveCustomFilterMethods, SieveCustomFilterMethods>();.

Now you can send requests in the form:

& AuthorIsAnyOf == Bad Pitt|Mel Gibson

Note Im using the pipe character (|) instead of a comma because Sieve uses commas to separate filter terms.
  • 另一个应用OR逻辑的例子:
    技术图片
    You can do that with a custom filters so for example:
    
    The equivalent of your (title, body, author)@=ABC would be [email protected]=ABC with:
    
    public class SieveCustomFilterMethods : ISieveCustomFilterMethods
    {
        public IQueryable<Post> PostSearch(IQueryable<Post> source, string op, string value)
        {
            if (op == "@=")
            {
                var result = source.Where(p => p.Title.Contains(value)
                                || p.Body.Contains(value)
                                || p.Author.Contains(value));
                return result;
            }
            else
            {
                return source;
            }
        }
    }
    
    This would save you having to send requests with the same property names repeated every time, as well as allow for any additional logic you might need.
    
    That being said, I understand the need for OR logic, especially when you need to query unknown arbitrary parameters each time. I was working on an version where a | would donate OR (similar to , currently denoting AND). I think this would allow for more complex logic than your suggested implementation - at the cost of being less concise and more redundant (ie. instead of (p1,p1)@=ABC, youd have [email protected]=ABC|[email protected]=ABC. Let me know what you think - would that work for your use case or am I missing something?
    
    Really glad you like Sieve :D.
    View Code

     

以上是关于asp.net core 排序过滤分页组件:sieve的主要内容,如果未能解决你的问题,请参考以下文章

asp.net中GridView怎样进行分页,编辑,删除操作

如何使用 ASP.NET Core 从查询字符串中读取值?

在 Razor 视图中使用 Ajax 进行 Asp.Net Core 表分页

带有 ASP.NET MVC 和 AJAX 的分页表 [关闭]

在 ASP.NET Core 2.1 Web API 中实现分页

ASP.NET 中的分页;过滤后页数不变