Query object pattern
You may not find much mention of query object pattern elsewhere. This pattern is very abstract and most people have their own interpretation and implementation of it. The one I am going to present here is an implementation that I have used successfully. At the heart of query object pattern is the concept that everything of a query (unlike only the filtering criteria in specification pattern) is abstracted away behind an interface so that it can be reused. The simplest form of this interface would look as follows:
public interface IQuery<T> where T : EntityBase<T> { IEnumerable<T> Run(IQueryable<T> queryable); }
IQuery<T>
is a generic interface where T
stands for the entity type which is being queried. The Run
method on the interface is what runs the query and returns an IEnumerable<T>
as a result. Now, unlike specification pattern, the Run
method takes in an IQueryable<T>
as input. This is where query object makes the difference...