Mentoring DDD: NHibernate 2 Fetching Strategies
Udi’s advice for 2.* users is to download Ayende’s Repository package and use it. Unfortunately my specific case did not allowed for that because my compandy developed an in-house abstraction later over O/RM and it would be very cumbersome to integrate it with repositories. So I was presented with choice of:
- giving up on using fetching strategies at all and face potential performance problems
- rolling back to NHibernate 1.2
- implemneting fetching strategies for NHibernate 2 myseft
As you can gues from title of this post, I have chosen the third alternative.
NHibernate 2 model for ISession implemention is totally differend from the one of 1.2 version: it is based on events. On one hand it allowed me to implement fetching strategies without modifieing NHibernate source code. On the other, it required me to rethink some concepts from Udi’s code.
Full source code can be downloaded from here.
Key concepts of this package include:
- As in Udi implementation, fetching strategies are defined by implementing generic interface IFetchingStrategy
where T is an interface which is requested from session to be fetched (for details read this gread post). - Fetching strategy implementations are found using helper class with an event rising when strategies are needed. It alows to register more than one source of fetching strategies. Common usage is, however, to wire the event with lookup in your favourite container or service locator, possibly through Common Service Locator.
The quality of this code is not yet production-ready, but it will increase during following days and as bugs will be found and fixed I will post new versions of the source code. Do not hesitate to ask any questions or inform about bugs/issues in comments of this post.



