مرتبسازی، فیلتر کردن و صفحهبندی اطلاعات در ASP.NET Core
نویسنده: سجاد آفاقی
تاریخ: ۱۳۹۷/۰۴/۰۷ ۷:۳۰
آدرس: www.dntips.ir
| مطالب | ۳۶۹۴ |
| نویسندگان | ۲۷۶ |
| گروههای مطالب | ۱۰۲۴ |
| نقشههای راه | ۱۱۹ |
| دورهها | ۱۴ |
| اشتراکها | ۱۷۹۱۴ |
// Post
public int Id { get; set; }
public string Title { get; set; }
public int LikeCount { get; set; }
public int CommentCount { get; set; }
public DateTimeOffset DateCreated { get; set; } = DateTimeOffset.UtcNow; services.AddScoped<SieveProcessor>();
۱.۳. از طریق اضافه کردن صفت (Attribute) به ویژگیها
// 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; ۲.۳. از طریق Fluent API
برای استفاده از این روش، ابتدا کلاسی را ایجاد و از کلاس SieveProcessor مشتق کنید. سپس تابع MapProperties موجود در کلاس والد را override کنید.// ApplicationSieveProcessor
public class ApplicationSieveProcessor : SieveProcessor
{
public ApplicationSieveProcessor(
IOptions<SieveOptions> options,
ISieveCustomSortMethods customSortMethods,
ISieveCustomFilterMethods customFilterMethods)
: base(options, customSortMethods, customFilterMethods)
{
}
protected override SievePropertyMapper MapProperties(SievePropertyMapper mapper)
{
mapper.Property<Post>(p => p.Title)
.CanFilter()
.HasName("a_different_query_name_here");
mapper.Property<Post>(p => p.CommentCount)
.CanSort();
mapper.Property<Post>(p => p.DateCreated)
.CanSort()
.CanFilter()
.HasName("created_on");
return mapper;
}
} services.AddScoped<ISieveProcessor, ApplicationSieveProcessor>();
[HttpGet]
public JsonResult GetPosts(SieveModel sieveModel)
{
var result = _dbContext.Posts;
result = _sieveProcessor.Apply(sieveModel, result);
return Json(result.ToList());
} GET /GetPosts ?sorts= LikeCount,CommentCount,-created // sort by likes, then comments, then descendingly by date created &filters= LikeCount>10,Title@=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
| عملگر | توضیحات | عملگر | توضیحات |
| == | برابر | =@ | شامل |
| =! | مخالف | =_ | شروع شود با |
| < | بزرگتر | *=@ | شامل (حساس به حروف)* |
| > | کوچکتر | *=_ | شروع شود با (حساس به حروف) |
| =< | بزرگتر مساوی | *== | برابر (حساس به حروف) |
| => | کوچکتر مساوی | |
{
"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"
}
} services.Configure<SieveOptions>(Configuration.GetSection("Sieve"));