Skip to content

Usage ‐ Predicate Factory

Bert Loedeman edited this page Oct 10, 2017 · 2 revisions

The predicate factory converts a given data filter into a DapperExtensions predicate group object. If you are unfamiliar with the concept, please read more about predicates on the DapperExtensions Wiki page. It does so by parsing filtering metadata. For the BookDataFilter object, this looks like:

internal class BookFilterMetadataProvider : IFilterMetadataProvider
{
    Type IFilterMetadataProvider.Type => typeof(BookDataFilter);

    IList<FilterMetadata> IFilterMetadataProvider.Metadata { get; } = new List<FilterMetadata>
    {
        new FilterMetadata<BookDataFilter, BookData>
        {
            FilterExpression = data => data.Id,
            FilterType = Operator.Eq,
            FilterValue = dataFilter => dataFilter.BookId,
            DefaultValue = default(int?)
        }
    };
}

The predicate factory scans metadata and adds predicates if they apply and the FilterValue function does not result in the metadata default value.

Sample

  • Consider a filter with BookId = 1.
  • The filter value function will return 1. This does not equal the default value, default(int?).
  • The filter expression will tie the result to the Book.Id property.
  • Since there is a valid situation, a predicate will be added to a predicate group, which will be returned by the predicate factory.
  • Dapper(Extensions) will use the returned predicate to create the following select query: SELECT * FROM Books WHERE Id = 1.
Clone this wiki locally