Book Image

Learning NHibernate 4

Book Image

Learning NHibernate 4

Overview of this book

Table of Contents (18 chapters)
Learning NHibernate 4
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

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...