- Posted by liammclennan on April 18, 2009

Today I started experimenting with NHibernate. As I have become more interested in TDD, BDD, SOLID, DDD and other upercase acronyms I am incre
asingly seeing NHibernate as the only viable option for persistence. NHibernate will be the third ORM tool that I have learnt. The first two were Wilson ORMapper and Linq-to-sql. Unfortunately for me, both Wilson ORMapper and Linq-to-sql have been killed off by their respective authors. Initially I thought that because I understand ORM tools learning NHibernate would be trivial, but it has turned out to be far more complicated than either Wilson ORMapper or Linq-to-sql.
So far I do not like NHibernate. It is the opposite of convention over configuration. There are ten different ways to do anything, which contributes to its already poor and distributed documentation. It is great to have choices but in the case of NHibernate I think it really weakens the experience. At the very least there should be some kind of out-of-the-box experience that uses a default strategy that just works (ala RoR). Here are some of the areas where a user has to choose how to use NHibernate:
- Mapping Strategy - NHibernate supports mapping by attributes, xml files or a fluent interface. The fluent interface seems to be the way to go but I could not get it to work. Even the demo app on the fluent nhibernate site does not work. Therefore, I went back to the xml mapping strategy.
- Configuration - Configure in code, hibernate.cfg.xml file, app.config or the fluent interface.
- Query Strategy - There are three different techniques for querying: HQL, Criteria (detached and attached) and Linq-to-Nhibernate. From what I can gather Linq-to-Nhibernate is not really production ready. HQL does not have any compile-time checking so I am leaning towards a query strategy based on detached criteria.
- Session Management - I will be using NHibernate in a web environment so I plan to implement the session-per-request pattern. This means that my ISessionFactory will be application scope and ISession will be scoped to an individual HTTP request.
- Architectural Patterns - Ayende's excellent article Repository is the new Singleton describes two different data access patterns that both apply to NHibernate: DAO/Repository and Specification. He contends that DAO services don't provide any value and we should access ISession directly and encapsulate querys into query objects (or specifications). I'm not sure what to do here.
The book, NHibernate in Action is the most useful NHibernate documentation that I have. Its obsolescence shows NHibernate's rapid recent change. Despite the book being only a few months old it is hopelessly out of date. The book documents NHibernate 1.2 but NHibernate 2.0 is the current version. Major features such as fluent nhibernate and Linq-to-nhibernate are not mentioned in the book.
Maybe I'll try the entity framework...