AutoFilterer Documentation

repository·develop·Indexed 19 days ago

https://github.com/enisn/autofilterer

A .NET library that automates the creation of LINQ expressions for filtering entities using DTOs. Designed as a lightweight, OpenAPI 3.0 compatible alternative to oData or GraphQL, it allows developers to generate complex queries for IQueryable without manual LINQ writing. Includes extensions such as AutoFilterer.Swagger for interactive API documentation, AutoFilterer.Generators for source-generated filter objects, and AutoFilterer.Dynamics for untyped filtering using dictionaries and JSON.

Tokens
22.7K
Snippets
86
Records
108
Agent score
62%

What's inside AutoFilterer

  1. Overview of AutoFilterer

    develop
    AutoFilterer is a filtering framework for .NET designed to automatically generate LINQ expressions from DTOs (Data Transfer Objects) for entities. It allows developers to create complex queries for IQueryable without manually writing LINQ expressions. Unlike oData or GraphQL, AutoFilterer's parameters and usage are designed to be compatible with the OpenAPI 3.0 Specification.
  2. Control parameter combination logic with CombineWith

    develop

    By default, multiple filtering parameters are combined using the AND (&&) operator. You can change this behavior to use the OR (||) operator by setting the CombineWith property on your filter DTO.

    If you are using Swagger, CombineWith appears as an enum parameter:

    • 0 (or CombineType.And): Combines parameters with && (Default).
    • 1 (or CombineType.Or): Combines parameters with ||.

    This allows you to switch between strict filtering (all conditions must be met) and broad filtering (any condition can be met) dynamically.

    // Default behavior (AND):
    // Query: /Blogs?priority.min=4&isPublished=false
    // Result: x => x.Priority > 4 && x.IsPublished == false
    
    // OR behavior:
    // Query: /Blogs?priority.min=4&isPublished=false&combineWith=1
    // Result: x => x.Priority > 4 || x.IsPublished == false
  3. Implement custom filterable types with IFilterableType

    develop

    You can define custom complex types (like a Range<T> object) that have their own specific expression generation logic. To do this, create a class that implements the IFilterableType interface and provide your own implementation of the BuildExpression() method. AutoFilterer will automatically use this method when it encounters the type in a filter DTO.

    // Example pattern for a custom type
    public class MyCustomType : IFilterableType
    {
        // Implement BuildExpression to define how this type translates to a LINQ expression
        public Expression BuildExpression(Expression expressionBody, PropertyInfo property, object value) 
        {
            // Custom logic here
        }
    }
  4. Use OperatorFilter for dynamic numeric and comparable comparisons

    develop

    The OperatorFilter<T> is used to perform dynamic comparisons on types that implement the IComparable interface (such as numeric fields). It implements IFilterableType, allowing it to be used as a property type within a FilterBase object. Unlike static filters, both the comparison operator and the comparison value are provided by the client via the query string.

    public class BookFilter : FilterBase
    {
        public OperatorFilter<int> TotalPage { get; set; }
    }
  5. Compare OperatorComparison vs StringFilterOptions

    develop

    It is important to understand the difference in how queries are generated for string properties:

    1. StringFilterOptions (Default behavior): Often generates queries using method calls, for example: x => x.Title.Equals("something", StringComparison.InvariantCultureIgnoreCase). This may fail on providers like MongoDB.
    2. [OperatorComparison] attribute: Generates queries using direct operators, for example: x => x.Title == "something". This is the recommended approach for database providers that do not support the .Equals() method translation.
  6. Use untyped filtering with AutoFilterer.Dynamics

    develop
    While the core AutoFilterer library relies on strongly-typed classes to ensure type safety, the AutoFilterer.Dynamics extension allows you to use the framework without predefined types. It achieves this by using dictionaries and plain JSON instead of class-based DTOs.
  7. Create a custom filterable type by implementing IFilterableType

    develop
    You can define custom types (like a Range<T> object) that have their own specific expression generation logic. To do this, create a class that implements the IFilterableType interface and provide your own implementation for the BuildExpression() method. AutoFilterer will automatically use this method when it encounters the type in a filtering DTO.
  8. How AutoFilterer works with LINQ and DTOs

    develop

    AutoFilterer is a filtering framework for .NET designed to automatically generate LINQ expressions for Entities based on DTO (Data Transfer Object) filter models.

    Key Concepts:

    • LINQ Expression Generation: The library does not generate database queries directly; instead, it generates LINQ expressions that you can apply to an IQueryable (e.g., an Entity Framework DbSet).
    • DTO Mapping: You define a filter model (DTO) where property names should match the corresponding Entity properties.
    • OpenAPI Compatibility: Unlike oData or GraphQL, AutoFilterer is designed to be compatible with OpenAPI 3.0 specifications.