An expression tree is a mechanism that allows developers to create expressions that are necessary for the filters in the queries. In .NET, we have Func<T, TResult>
to wrap a Where
predicate and use it in multiple occurrences. We could use the same mechanism to create expression trees and leverage them in query objects.
The generic IQueryExpression
interface has a provision to create an expression through Func
, the following code creates contract for AsExpression()
.
public interface IQueryExpression<T>
{
Expression<Func<T, bool>> AsExpression();
}
The concrete expression class implements IQueryExpression
with concrete methods and their interface counterpart and wraps the Where
predicate inside the AsExpression
method, which returns a Func
object. The following code provides a wildcard search on all fields from the Post
entity using the expression approach:
public class GetPaginatedPostByKeywordQueryExpression :...