BT

Facilitating the Spread of Knowledge and Innovation in Professional Software Development

Write for InfoQ

Topics

Choose your language

InfoQ Homepage News Debate: Comparing NHibernate and EF 4

Debate: Comparing NHibernate and EF 4

This item in japanese

Recently, a post by Oren Eini (a.k.a. Ayende Rahien) touched off a debate around the respective merits and capabilities of NHibernate and Entity Framework 4.0, two .NET-based Object/Relational Maping frameworks. InfoQ explored this debate in more detail to understand some of the perspectives which were given.

Rahien, a member of the NHibernate project, made a quick comparison between NHibernate and Entity Framework 4 (EF). After appreciating the progress EF 4 has made in regards to EF 1.0, Rahien enlisted several features that, in his opinion, make NHibernate a better ORM solution:

  • Write batching – NHibernate can be configured to batch all writes to the database so that when you need to write several statements to the database, NHibernate will only make a single round trip, instead of going to the database per each statement.
  • Read batching / multi queries / futures – NHibernate allows to batch several queries into a single round trip to the database, instead of separate roundtrip per each query.
  • Batched collection loads – When you lazy load a collection, NHibernate can find other collections of the same type that weren’t loaded, and load all of them in a single trip to the database. This is a great way to avoid having to deal with SELECT N+1.
  • Collection with lazy="extra" – Lazy extra means that NHibernate adapts to the operations that you might run on top of your collections. That means that blog.Posts.Count will not force a load of the entire collection, but rather would create a "select count(*) from Posts where BlogId = 1" statement, and that blog.Posts.Contains() will likewise result in a single query rather than paying the price of loading the entire collection to memory.
  • Collection filters and paged collections  - this allows you to define additional filters (including paging!) on top of your entities collections, which means that you can easily page through the blog.Posts collection, and not have to load the entire thing into memory.
  • 2nd level cache – managing the cache is complex, I touched on why this is important before, so I'll skip if for now.
  • Tweaking – this is something that is critical whenever you need something that is just a bit beyond what the framework provides. With NHibernate, in nearly all the cases, you have an extension point, with EF, you are completely and utterly blocked.
  • Integration & Extensibility – NHibernate has a lot of extension projects, such as NHibernate Search, NHibernate Validator, NHibernate Shards, etc. Such projects not only do not exists for EF, but they cannot be written, for the most part, because EF has no extension points to speak of.

As advantages of using EF 4, Rahien mentioned:

  • EF 4.0 has a better Linq provider than the current NHibernate implementation. This is something being actively worked on and the NH 3.0 will fix this gap.
  • EF is from Microsoft.

Being a known contributor of the NHibernate project, Rahien's post triggered a considerable number of pro/cons reactions. A reader, tobi, complained on NHibernate’s poor error messages:

As someone who has worked with NHibernate for only a few hours it seemed to me that the experience of manually creating domain classes and mappings (with FluentNHibernate in my case) was quite manual and the error messages were not that good. This is something that I would write onto the EF4 side of the comparison.

Roy had mixed feelings about error messages and documentation:

An additional pro for EF is that the documentation is more organized and error messages are more descriptive.
I still prefer NH though, but it takes a lot of blog-browsing if you have a problem. Conversely, the benefit of that might be that there are a lot of people you can ask questions to.

Jimmy Bogard appreciated how NHibernate’s bug fixing process makes it more alluring:

Another major advantage of NH is that it's OSS. I've needed to patch NH several times over the years, to fix a bug or add something I needed. With EF, I'm just stuck.

Alex Yakunin, involved in creating the ORMBattle.NET test suite, another ORM tool, complained:

I think you should explicitly state that only benefits of NHibernate are shown here. Disadvantages are almost untouched at all - even your words about LINQ provider are pretty far from truth; another well-known case is that EF supports change tracking, but NH does not, and this can dramatically affect on performance in set of cases (in fact, you should forget about certain cases in NH at all - just "by design").

Radenko Zec compared unit testing and designer capabilities:

I think that biggest advantage of NHibernate is better support for unit testing. EF4 is not designed for testing and it is very difficult to write unit tests for some custom solution based on EF4.
On another hand EF4 has very good designer (most important thing that you need for real world large project) and POCO T4 template based on that designer... I think that is time that you start thinking about making your own designer for NHibernate instead of denying need for designer and code generator. If community wants designer for NHibernate, give them good designer... And third party designers are miles away from EF4 designer except probably LLBGEN 3 that is not yet released and is not free.

Frans Bouma, the man behind LLBLGenPro, yet another ORM tool, mentioned NHibernate lacking badly when it comes to documentation:

What EF has over NH is documentation, solid examples and a large stream of evangelists speaking at every developer conference there is and pumping out articles around the clock. … NH sucks in that department (and please, don't come with excuses, it does suck in the documentation department. If you want to know how big it sucks in that department, take a tour in the DDL SQL producing docs for _N_hibernate and learn how great it can generate ... java classes. huh?), and at the same time has many options, it's not really an advantage to have.

Felix proposed a combined solution:

Don't remember who said ... "OR/M is the Vietnam of the Coding"... NH is a veteran, EF is young recruit. Unfortunately MS can't leverage OS, if they do life would be easier: designer and integrated tooling by MS, OR/M by NH would be a productive solution.

The general consensus appeared to be that NHibernate has a number of features which are missing from Entity Framework 4.0 like read/write batching, "extra" laziness, collection filters, tweaking, and others, however Entity Framework has a better LINQ provider, documentation and it is supported by Microsoft. What are your thoughts on this debate?

Rate this Article

Adoption
Style

BT