public class QueryFactory : IQueryFactory
private readonly IUnityContainer container;
public QueryFactory(IUnityContainer container)
this.container = container;
public TQueryType Create<TQueryType>() where TQueryType : IQuery
TQueryType query = container.Resolve<TQueryType>();
The trick is that every query takes an NHibernate ISession that it uses to do its work. I have that session delivered up by the Unity container so all the Query has to do is specify that it needs ISession by taking one it its constructor and it gets the
session for this request. Queries can also take addition parameters if they need. Most don't need any but some need to do operations based on when Now is so they take an IClock which is a simple interface that has a single property Now. I use this instead
of DateTime.Now to make things easier to test and to ensure that time is tightly controlled in the application. I even have a specialty clock that returns Now as the most recently passed 5 minute mark (10:00, 10:05, etc). I use this for checking things like
Published on an Entry or Banned on a user so that the query will cache better.
memcached. I'll talk on Monday about how I do elastic scaling of the cache nodes. It's pretty well known stuff but it integrates well with Azure.
But the cache model is fully abstracted from the rest of the code. In fact, in dev it uses the ASP.Net cache and swaps to memcached in production. If we ever wanted to change to AppFabric, it wouldn't be very hard.
I don't write comments pretty much ever. When I TDD (which I do for everything these days), I don't need them. What I was thinking and what I meant for the code to do is much better expressed through good names, solid patterns, and great tests. I can even
read the tests sequentially and watch the class evolve. Ditch those comments and up your code reading skills! It makes you a much better developer.