Trying to choose between NHibernate and Entity Framework?

entity-framework nhibernate orm


What are the main advantages and disadvantages of NHibernate and Entity Framework 4.0?

(While tagging my question, I've noticed more NHibernate tags than EF. Is NHibernate more popular?)

7/20/2010 1:20:13 AM

Accepted Answer

I've had very limited experience with EF (a few blog posts and tutorials), but plenty of experience with NHibernate.

Microsoft is consistantly behind where the OpenSource movement is, and this is definitely the case with NHibernate. When EF 1.0 was first released, it lacked support for key features such as POCO support and persistence ignorance, it required your entity classes to extend a base entity class from EF, so your entity classes weren't completely decoupled from EF. This effected maintainability and unit testability, as well as the ability to switch ORM providers if need be.

NHibernate on the other hand allowed you to completely decouple your entities from NHibernate itself. EF 4.0 has caught up somewhat in this regard, it's Code First Development approach (recently announce by Scott Guthrie) is a step in the right direction.

NHibernate itself has some very powerful mapping capabilities such as Inheritance mapping, mapping of component classes (think Address object on a Customer entity), unidirectional entity association mapping. It lets you take full advantage of the power of Object Oriented programming (Encapsulation, Polymorphism, Inheritance etc.) inside your domain model, think of your entity classes as encapsulating behavior and not just data containers. If you're aspiring to build your domain model to the principles of Domain Driven Design, then NHibernate is a very good fit for this. I can't speak directly on EF 4.0 on these points, only from what I hear from other NHibernate users trying to use EF 4.0, but EF 4.0 still lags behind in these areas, but it's gaining ground.

7/20/2010 1:04:55 PM

Popular Answer

I'd say the fact that Entity Framework is from Microsoft is both an advantage and a disadvantage. You're getting a framework right from the same source as .NET itself. The bad news is that Microsoft often obsoletes its own code base without regard for backwards compatibility.

NHibernate does not come from Microsoft, and there's no standard other than what the developers of Hibernate and NHibernate say belong in their code. The good news is that they've generally paid attention to backwards compatibility. There's a large user base, because Hibernate has been around for a while.

One of the features I like best about Spring is that they don't take decisions like this away from you. Spring has its own JDBC features, but it supports Hibernate, TopLink, JDO, iBatis, and JPA. You're also free to inject your own classes if you decide to go another way (e.g., NoSQL). Why should your choices be dictated by the framework choice? It's very nanny-ish: "We're Microsoft; we know what's best for you better than you do."

Related Questions


Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow