Entity Framework vs NHibernate

I have worked with both Entity Framework (at work) and NHibernate (at private projects) for a while now, and I tought I would write a post about the differance between the two. They are both (kind of) Object Relational Mappers (ORM´s) and they were developed to solve the same problem, that is data access in a .NET application. Entity Framework is a product from Microsoft while NHibernate is a Open Source project.

As everyone who have built a data dependent application knows, data access code can be pretty repetative. You do the same thing over and over again in application after application. For this reason we have ORM´s, they abstract the unnecessary repeating of ADO.NET code away from the application and we can instead query our domain model in the way we prefer. In my oppinion, a application developer should not have to think about the database model, we should leave that to the dba. A ORM eliminates this need as we just have to create our domain model and query it as a abstraction of the acctuall data storage.

NHibernate

The differance between the two frameworks, nhibernate and entity framework, is pretty big. With NHibernate you map your entities to your database model with xml files. You have one xml file for each class you want to map where you specifie how the object will be persisted and what relations it has to other objects and how they are related. You then have a xml file for configuration where you tell NHibernate where to find the mappings you have specified, what kind of database to use, what connectionstring to use and a few more options.

Then when you want to query your objects you use a ISession that basicly is a connection to your database. There is four different ways to query with NHibernate out of the box. You can use hql (wich is a sql-like language that queryes the object instead of a table), criteria (wich is a fluent interface to chain method calls and specifie different criterias for the query), stored procedures and normal sql queries. They do all have different pros and cons. The most ORM like way to query is using the Criteria api. This is a high level abstraction and the most easy to understand for most developers. While at the low level you have stored procedures and normal sql queries. At a lower level you do offcourse have more control of what data to get and you can optimice your queries more then with a high level abstraction. In most senarious a high level approach is preferable as it´s easier to maintain and do not have as many dependencies as the low level queries have.

A good thing with NHibernate is that it is a very mature framework with a Java version (Hibernate) that has been out for a long time. There is also some very good extensions for NHibernate. You have FluentNHibernate that makes mapping easier with a fluent interface where you map with c# (or the language of youe choise) code.  You have a implementation of Linq for NHibernate that enables querying with Linq if you prefer that over the other options. You have NHibernate search that uses Lucene.NET, a full text search engine, to enable free text search with good preformance. This among other libraries make NHibernate a very flexible framework with alot of options to suit many developers.

Entity Framework

With Entity Framework you have a more visuall mapping technique where you have a ".edmx" file that you use to visually map your database. You can here specifie what database to use and you will have the option to import the tables that you want. Entity Framework will then generate your domain model from this and create classes with the appropriat relationships. This is a very easy and fast mapping that will be done very quickly. The NHibernate mapping will take more time to complete and there is more that you have to do on your own. Even if you have a library like FluentNHibernate that helps reduce the coding alot with some conventions. The problem with Entity Frameworks easy mapping is that it does only support MS SQL Server while NHibernate supports all the popular databases. On the other side, the acctuall access to the database and query generation can be a bit more optimiced with Entity Framework as you only have one database to think about, and they are both Microsoft products.

To query your objects with Entity Framewok you use Linq or stored procedures. You do this by creating a instance of the data context that will be generated with your domain model. The context will then contain you entities ready to be queried. If you have stored procedures you can map them in the same way you map your tabels. The data context will then contain a method with the same name as the stored procedure with the requiered parameters and Entity Framework will define a object that will be returned from the method that calls the stored procedure. This is a very easy-to-use approach that I like, even if I dont use stored procedures very often.

One thing to note here is the options for lazy loading. Lazy loading is where you load a refering object/collection at the time when you use it, and not before or when you dont need to use it. With NHibernate this is very easy. You only have to declare your properties of your entities as virtual and NHibernate will take care of the rest. You can use your entities in any way you want and not notice what is acctually happening in the background. With the current version of Entity Framework however, there is no implementation for lazy loading. This will however be present in the next version that is to be shipped with visual studio 2010 later this year. With the current version of Entity Framework you will instead explicitly define what releations to load each time you query a object. You can do this eather by using the ".Include" method and specifie the name of the object or collection that you want to include in your query. Or you can use a nested query as described in this post.

Conclution

I feel that NHiberate is a more mature framework as of the current versions. And it does, in my opinion, follow the general principals of object oriented programing alot better. Entity Framework isn´t realy a ORM in the same sence as NHibernate. While NHibernate includes all the features that you can expect from a full grown ORM framework, Entity Framework is more like just a abstraction of the database itself in the current version. I feel that, right now, NHibernate is a more complete framework that includes the features that I look for in a ORM. In my oppinion, Entity Framework isn´t quite there yet. Lets see what will happen in the next version that will ship later this year.

kick it on DotNetKicks.com
Shout it

Comments

Add comment

Name:

Mail:

Website:

Comment:


<< [1] >>
  • 9 months ago by Chris Love

    Actually, with Entity Framework there are providers available for most popular databases, like Oracle; http://code.msdn.microsoft.com/EFOracleProvider. I know there are others, like DB2 and MySQL for example. It is designed to allow you to utlize and data store you have a provider for. I have not written a provider myself, but have been told it is fairly easy. I know several of these providers were available from those vendors when EF first shipped. Linq To SQL on the other-hand is limited to SQL Server.

    As far as how you program against the Entity Framework model you can write your queries in Linq to Entities (which is why I generally prefer) or Entity SQL. Entity SQL is a T-SQL like syntax, but instead of querying against database objects you are querying the data model instead.

    You can also manipulate the model definition in the edmx file by hand if you want. My experience has shown this to be very over time-consuming and I have not found anything I could not do in the designer anyway. It just may not be naturally obvious to you at first.

    You can do lazy loading in EF as well and even POCO, but for my needs so far I have not really had a situation where I needed to get too deep with either one...yet ;)


    Reply | Hide replys

    • 9 months ago by Jakobsson

      I didn´t acctually know that you could write providers for other databases then MS Sql. I have a bit more experiance with NHibernate so far. I know you can edit the ".edmx" file as you want. The thing about the Entity Framework designer that I dont like, is the code it generates. It´s a lot of messy code. Even if it is generated code, I like to be able to read it.

      The problem with editing the generated code is also that you will have to do it every time you regenerate the code.

      If you by lazy loading mean calling the "Load()" method, I wouldn´t realy call that lazy loading. Atleast not in the same sence as NHibernate or other ORM´s does. In my oppinion, implicit lazy loading is more effective then explicit.

      I admit that I have alot more experiance with NHibernate then with Entity Framework, this will offcourse effect my choise of what ORM I like the most. But so far I haven´t realy found any area that Entity Framework handles better then NHibernate does.


      Reply

  • 9 months ago by Kaj Bonfils

    I heard that the generation of code will be handled by templates in the next version of the entity framework, utilizing the T4 code generation tool.. This way, you'll be in much more control over the generated code.

    I agree that NHibernate is a much more mature product. Im looking forward to see the next version. I has some promising capabillities.


    Reply

    • 9 months ago by Scott White

      Good analysis.  I'm sorry EF folks but if you go down the list of NHibernate features EF is way behind and they realy aren't focusing on the pure ORM crowd it seems.

      One other plus with NHibernate is that your experience with NHibernate translates very easily to Hibernate for Java.  EF really doesn't translate to any other ORM so the skills are not as portable.

      Event with adapters for other databases, they are not as mature as NHibernates support of other databases.  Additionally NHibernate supports more RDBMS systems.


      Reply | Hide replys

      • 9 months ago by Jakobsson

        Thank you. I agree that EF isn´t realy a ORM in the same way as NHibernate. I´m not quite sure what to acctually call it. To me it is more like a middle way between regular ADO.NET code and a pure ORM.

        When I write queries with Entity Framework I still feel like I´m querying the database with a strongly typed language instead of sql. When I query with NHibernate I dont feel like it´s acctuall database that I query. That is, in my oppinion, the way that a ORM should work. A good ORM makes you think object first.



        Reply

    • 1 months ago by Omar

      EF4 now does support Lazy loading see article:

       

      http://thedatafarm.com/blog/data-access/a-look-at-lazy-loading-in-ef4/


      Reply

      << [1] >>