Class MaybeQueryInterceptor
- Namespace
- Trellis.EntityFrameworkCore
- Assembly
- Trellis.EntityFrameworkCore.dll
An EF Core query expression interceptor that automatically rewrites Maybe<T> property accesses in LINQ expression trees to their underlying storage member equivalents.
public sealed class MaybeQueryInterceptor : IQueryExpressionInterceptor, ISingletonInterceptor, IInterceptor
- Inheritance
-
MaybeQueryInterceptor
- Implements
- Inherited Members
- Extension Methods
Examples
With this interceptor registered, these LINQ expressions work directly:
// Natural LINQ — no explicit extension methods needed
context.Customers.Where(c => c.Phone.HasValue)
context.Orders.Where(o => o.SubmittedAt.GetValueOrDefault(DateTime.MaxValue) < cutoff)
// Specifications with Maybe<T> properties
public override Expression<Func<Order, bool>> ToExpression() =>
order => order.Status == OrderStatus.Submitted
&& order.SubmittedAt.GetValueOrDefault(DateTime.MaxValue) < _cutoff;
Remarks
When registered, this interceptor allows natural LINQ syntax with Maybe<T>
properties in queries and specifications, without requiring explicit extension methods
like WhereHasValue or WhereEquals.
Register by calling optionsBuilder.AddTrellisInterceptors() — that helper
installs both this interceptor and the companion
Trellis.EntityFrameworkCore.MaybeEvaluatableExpressionFilterPlugin required for correct
c.Maybe == Maybe.From(value) translation. Registering only the interceptor via
optionsBuilder.AddInterceptors(new MaybeQueryInterceptor()) is sufficient for the
rewrite patterns listed above, but leaves natural-form equality against
Maybe.From(value) silently translating to _field IS NULL.
Methods
QueryCompilationStarting(Expression, QueryExpressionEventData)
Rewrites the query expression tree before compilation, replacing Maybe<T> property accesses with EF Core-translatable storage member references.
public Expression QueryCompilationStarting(Expression queryExpression, QueryExpressionEventData eventData)
Parameters
queryExpressionExpressioneventDataQueryExpressionEventData