Thursday, February 04, 2010

Comparing NHibernate and EF 4

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.

Read more: InfoQ: Debate

Posted via email from jasper22's posterous