Reasons to like NHibernate and ORM

Ayende Rahien, a contributor to NHibernate along with many other open source projects, has written up a post listing the features he likes about the ORM tool.

I am a fan of ORM and have used NHibernate extensively on a large project over the last few years.  Prior to that, I had used TopLink and Hibernate on java projects.

Ayende’s listing pretty much summarizes why I like NHibernate and also has points that apply to many other ORM products as well:

  • Caching.  NHibernate is specifically flexible in this area but many ORM products excel in this area over sql based DAL implementations.
  • Multi-database support.  Using an ORM insulates your code from the differences in database dialects.  This might sound insignificant, but if you have ever had to migrate any application from Oracle to SQLServer, for instance, you will know what a chore it can be.
  • A dramatic reduction in plumbing code for your application.  Although he does not directly say this in his list, he does mention writing applications with no sql in them.  What I’ve noticed is that there is a ton of code that gets written in your application just to put data in your domain model and pull it back out.  Using an ORM removes this code from your application and puts in the domain of the ORM, which is code someone else writes and tests.

Using an ORM seems to encounter a great deal of resistance from some enterprise developers.  Below are a few of the reasons that are usually given against using an ORM product on a project:

  • Even though you are not writing sql, you still have to perform the mapping.
  • The sql generated by the ORM cannot be optimized for specific cases and will be slower.
  • Using reflection is slow.
  • Using an ORM makes the data access problems more difficult to debug.
  • Everyone knows sql and nobody knows <ORM product>.

Even though are not writing sql, you still have to perform the mapping

This is a good point.  It is true that the work of mapping data to columns in a database is moved from sql to the mapping layer of the ORM.  In the case of NHibernate, this would be XML files.  So in that instance, no work or time is saved.  Where the time savings comes in to the picture is when the actual mappings are used.  NHibernate takes those mapping files and performs the domain object instantiation and population for you. 

Detractors of the ORM approach might counter with the argument that the usage of the ADO.net data constructs, for instance the typed dataset, also removes the need to write object population code.  Although this might be true, I believe that passing ADO.net data types around your application leads to poor application architecture, not to mention performance problems.  Typed datasets are notoriously slow when it comes to serialization.

The sql generated by the ORM cannot be optimized for specific cases and will be slower

This might be true for certain ORM products, but has not been my experience with NHibernate.  The generated sql is quite good.  However, I have encountered situations where the sql has been unacceptably slow and in those cases I can almost always place the blame on a poorly designed database model.

NHibernate has the ability to pass sql to the database, if needed.  SQL optimizations can also be placed in the mapping files to help in situations where NHibernate could not generate the optimal queries.  Named Query support, added to NHibernate 1.2, has helped as well for specific query situations that might not be the normal path for searching for a given object type.

Using reflection is slow

This is always an interesting discussion to have with a stubborn developer.  Yes, it is true that a method invocation via reflection would be slower than a method invocation compiled into the IL directly.  The tradeoff you are making is flexibility for a little speed.  The reality of the situation is that the slowdown for reflection will be dwarfed by the IO wait for the database and you will not notice it. 

NHibernate also has functionality called the reflection optimizer which will dynamically generate classes for populating your domain objects at startup time.  This a tradeoff in startup time to remove the cost of reflection for each domain object population.  Early versions of the reflection optimizer were noticeably slower at startup for larger numbers of mapped classes, but this has been corrected in the newer versions of NHibernate.

Performance problems in enterprise applications are caused by poor database design or poor approaches to data retrieval in 99% of cases.  I should also lump screens that have too much data on them in with this category as well.  The other 1% of the time the performance problems are caused by poorly written threaded code, poor algorithms, and poor integration choices with 3rd party applications.  I have never encountered an enterprise application that was too slow because of reflection usage.

Using an ORM makes the data access problems more difficult to debug

I hear this complaint a lot at the beginning of projects specifically from developers that have never used ORM before.  I would say that the real statement that should be made about the subject is:

Using an ORM makes the data access problems different to debug.

Most of time, a developer simply needs to familiarize themselves with the debugging facilities available in the ORM tool they are using.  There is cost to learning what is available, just as there is a cost up front for learning any new tool or concept, but the productivity that ORM gives a project overall will make up for the learning curve.

Everyone knows sql and nobody knows <ORM product>

I also hear this complaint a lot at the beginning of project where I propose using ORM.  It is difficult to convince someone that ORM will payoff after the learning curve is overcome when they have never used an ORM product.  There are many blog posts on the web complaining that ORM does not pay off in the long run but this simply has not been my experience.

The response I usually give is to try the product for a pilot or proof of concept to test the pluses and minuses.  Most clients are won over when they see the benefits  of caching in the ORM and the ability to migrate databases with just minor mapping file changes (or maybe none).  Also, when they actually try using NHibernate or another ORM product, they find that the product is not quite as unapproachable from a technical standpoint as they thought when they first heard of the concept.

Tags: , ,

Comments

7 Responses to “Reasons to like NHibernate and ORM”

  1. Jason Haley on March 9th, 2008 9:40 am

    Interesting Finds: March 9, 2008…

  2. Reflective Perspective - Chris Alcock » The Morning Brew #49 on March 10th, 2008 12:21 am

    […] Reasons to like NHibernate and ORM – Darren Stokes replies to Ayende’s post with some more details on some points. […]

  3. Paul Roberts on April 6th, 2008 10:00 pm

    Theres also a bunch of commercial tools that take the mapping step out of it. Diamond Binding is around 500 bucks / free (depending on usage- wraps nhibernate and extends visual studio. Last release had a bunch of project templates as well – your setup effort is ticking boxes next to the tables you want, and then any time the schema changes you hit resync. It keeps its half of the mapped classes in partial files so your code doesnt get nuked.

    http://dunnchurchill.com/products/diamondbinding/

  4. Bagwani on November 3rd, 2008 4:14 am

    about reason one, quoting “Even though are not writing sql, you still have to perform the mapping”.

    There is a tool called NConstruct Lite which creates C# classes and the nhibernate XML mapping file automatically from a database.

    http://www.nconstruct.com/App/NConstructLite.aspx?PageId=12

  5. Tony on November 24th, 2008 4:17 am

    We’re also using NConstruct Lite for generating HBM and C# files so mostly we don’t bother with mapping hand-writing. There are special cases which is not (yet?) supported in a tool but we still get most code in a very short time. What is, in my opinion, even better – we don’t waste time with debugging because creating mapping files by hand could be quite buggy work.

  6. Ahmad on December 24th, 2008 11:14 am

    At my blog is my opinion of ORMs after having them used to build real world business applications beyond prototyping.

    I think ORMs have contributed in making developers think more object oriented but they loose points in ease of use, deployment, configuration, performance and isolation of data retrieval code. Read my blog for a proposed solution at
    http://theahmadblog.blogspot.com/

  7. Andrei Rinea on March 16th, 2010 4:44 am

    Oh yeah… does it work under medium trust?