AutoFilterer
AutoFilterer copied to clipboard
Select Expression Support
trafficstars
Summary
This feature aims to generate an expression for .Select() Linq method.
Declaration
See usage example for better understanding:
- Entity
public class Book
{
public string Name { get; set; }
public int TotalPage { get; set; }
public DateTime CreateDate { get; set; }
public virtual ICollection<BookOrder> BookOrders { get; set; }
}
- FilterDto (As always but inherits SelectorFilterBase)
public class BookFilterDto : SelectorFilterBase<BookDto>
{
[ToLowerContainsComparison]
public string Name { get; set; }
public Range<int> TotalPage { get; set; }
public Range<DateTime> CreateDate { get; set; }
}
- Output dto:
public class BookDto
{
[From("Name")]
public string Title { get; set; }
public int TotalPage { get; set; }
public DateTime CreateDate { get; set; }
[Count("BookOrders")]
public int SellCount { get; set; }
[Sum("BookOrders.Price")]
public float TotalSoldPrice { get; set; }
}
Usage
As default, SelectorFilterBase should override ApplyFilter() method, so calling ApplyFilter method will work.
[HttpGet]
public IActionResult GetBooks([FromQuery] BookFilterDto filter)
{
var query = db.Books.ApplyFilter(filter); // IQueryable<BookDto>
return Ok(query.ToList());
}
- Or alternative usage:
As default, SelectorFilterBase should override ApplyFilter() method, so calling ApplyFilter method will work.
[HttpGet]
public IActionResult GetBooks([FromQuery] BookFilterDto filter)
{
var query = db.Books.ApplySelector(filter) // IQueryable<BookDto>
.Where(x => x.TotalSoldPrice > 299.95); // If db provider supports.
return Ok(query.ToList());
}
Structure
- FilterBase
- OrderableFilterBase
- PaginationFilterBase
- SelectorFilterBase<T>
- PaginationFilterBase
- OrderableFilterBase
TODO List
- [x] Extension methods for last user usage
- [ ] SelectorFilterBase and SelectorPaginationFilterBase implementation.
- [ ] Attributes
- [ ] Sum
- [ ] Count
- [ ] Min
- [ ] Max
- [ ] Average
This might be useful: https://stackoverflow.com/questions/12701737/expression-to-create-an-instance-with-object-initializer/12705338#12705338
Dynamic Select is not compatible with Open API 3.0 standards. This is the main purpose of AutoFilterer. Only static select support can be provided.
- Also, this is a 'Filtering' library, it doesn't aim projection by the way