Using Query Classes With NHibernate

Posted by Liam McLennan on Geeks with Blogs See other posts from Geeks with Blogs or by Liam McLennan
Published on Sat, 12 Mar 2011 05:14:34 GMT Indexed on 2011/03/12 8:10 UTC
Read the original article Hit count: 251

Filed under:

Even when using an ORM, such as NHibernate, the developer still has to decide how to perform queries. The simplest strategy is to get access to an ISession and directly perform a query whenever you need data. The problem is that doing so spreads query logic throughout the entire application – a clear violation of the Single Responsibility Principle. A more advanced strategy is to use Eric Evan’s Repository pattern, thus isolating all query logic within the repository classes.

I prefer to use Query Classes. Every query needed by the application is represented by a query class, aka a specification. To perform a query I:

  1. Instantiate a new instance of the required query class, providing any data that it needs
  2. Pass the instantiated query class to an extension method on NHibernate’s ISession type.

To query my database for all people over the age of sixteen looks like this:

[Test]
public void QueryBySpecification()
{
   var canDriveSpecification = 
	  new PeopleOverAgeSpecification(16);
		
   var allPeopleOfDrivingAge = session.QueryBySpecification(canDriveSpecification);
}

To be able to query for people over a certain age I had to create a suitable query class:

public class PeopleOverAgeSpecification : Specification<Person>
{
  private readonly int age;

  public PeopleOverAgeSpecification(int age)
  {
      this.age = age;
  }

  public override IQueryable<Person> Reduce(IQueryable<Person> collection)
  {
      return collection.Where(person => person.Age > age);
  }

  public override IQueryable<Person> Sort(IQueryable<Person> collection)
  {
      return collection.OrderBy(person => person.Name);
  }
}

Finally, the extension method to add QueryBySpecification to ISession:

public static class SessionExtensions
{
  public static IEnumerable<T> QueryBySpecification<T>(this ISession session, Specification<T> specification)
  {
      return specification.Fetch(
          specification.Sort(
              specification.Reduce(session.Query<T>())
          )
      );
  }
}
The inspiration for this style of data access came from Ayende’s post Do You Need a Framework?. I am sick of working through multiple layers of abstraction that don’t do anything. Have you ever seen code that required a service layer to call a method on a repository, that delegated to a common repository base class that wrapped and ORMs unit of work? I can achieve the same thing with NHibernate’s ISession and a single extension method.

If you’re interested you can get the full Query Classes example source from Github.

© Geeks with Blogs or respective owner