$ show enisn/AutoFilterer
AutoFilterer is a .NET library that automatically generates LINQ expressions from Data Transfer Objects (DTOs) for filtering, sorting, and pagination on IQueryable collections. It is designed to be compatible with OpenAPI 3.0 specifications, unlike oData or GraphQL. The core architecture revolves around filter DTOs that inherit from FilterBase or its derivatives (PaginationFilterBase, OrderableFilterBase). Properties in these DTOs are decorated with attributes (e.g., StringFilterOptions, OperatorComparison, CompareTo) or use special types (StringFilter, OperatorFilter<T>, Range<T>) to define filtering rules. The library builds expression trees at runtime and applies them via the ApplyFilter extension method. It also provides a Swagger integration package (AutoFilterer.Swagger) to expose filter parameters in Swagger UI, and a source generator (AutoFilterer.Generators) to automatically create filter DTOs from entity classes. The codebase is well-structured with clear separation of concerns: abstractions (IFilter, IFilterableType), attributes, types, extensions, and a dynamic filter variant. It includes comprehensive unit tests, benchmark tests, and a sandbox application demonstrating usage with a Northwind database. The library targets netstandard2.0, netstandard2.1, and net9.0, making it broadly compatible. While the core library is mature and actively maintained, some ancillary packages (e.g., Swagger integration) use slightly outdated dependencies (Swashbuckle 5.x), and the documentation has some TODO sections. Overall, it is a solid, reusable filtering framework with high craftsmanship in expression tree manipulation.
The LLM identified 4 files worth highlighting for reuse:
A generic type that implements IFilterableType to provide dynamic operator comparisons (Eq, Not, Gt, Lt, Gte, Lte, IsNull, IsNotNull) for struct types. It builds individual expressions for each set property and combines them using a configurable CombineType. This component is self-contained and can be reused in any project that needs to generate comparison expressions from query parameters without coupling to AutoFilterer's full pipeline.
A type implementing IFilterableType for string comparisons. It exposes properties for Eq, Not, Equals, Contains, NotContains, StartsWith, NotStartsWith, EndsWith, NotEndsWith, IsNull, IsNotNull, IsEmpty, IsNotEmpty, and supports case-insensitive comparison via a Compare property. Each property generates the appropriate string method call expression (e.g., Contains, StartsWith) and combines them with a configurable CombineType. This is a highly reusable component for building string filter expressions from query parameters.
A generic type implementing IRange<T> and IFilterableType for range comparisons (Min/Max). It generates GreaterThanOrEqual and LessThanOrEqual expressions for the Min and Max properties respectively, handling nullable target properties correctly. It also supports implicit conversion from string (pipe-delimited format) and explicit conversion to string. This component is self-contained and can be extracted for any project needing range-based filtering on IComparable types.
An attribute that implements IFilterableType to generate binary expressions for a given OperatorType (Equal, NotEqual, GreaterThan, etc.). It handles nullable target properties by extracting the .Value property. The attribute also provides static instances for common operators (Equal, NotEqual, etc.) for reuse. This is a well-crafted, reusable attribute that can be applied to any property in a filter DTO to specify the comparison operator, and it can be used independently of AutoFilterer's full framework.