views:

2142

answers:

6

What are the advantages/disadvantages of using NHibernate ? What kind of applications should be (& should not be) built using NHibernate ?

+1  A: 

Advantages:

  1. Caching
  2. Simplicity in your code
  3. Power
  4. Flexibility
  5. Multi-database support

Disadvantages:

  1. Stops you having to write your own persistence code
  2. May reduce your knowledge of SQL

Applications you should use it for:

  • Any that use a database

A few more specific reasons to like NHibernate

Garry Shutler
I don't understand why 'Stops you having to write your own persistence code' is a disadvantage.
NerdFury
Some people like having to write more code than is necessary. It was a bit tongue-in-cheek.
Garry Shutler
I would add : Any that use a database - `but are not using it just for data manipulation : loading thousands of rows in memory `
sirrocco
How can it reduce knowledge of SQL? Not having to write own persistance code seems more like an advantage to me... Why would you want to reinvent the wheel anyways?Also by far not every application that uses database should be accessed through NHibernate...
Ray
A: 

The high level answer is that NHibernate is in a class by itself and there is no near competition.

If you need CRUD against a database from a .NET application, you should be using NHibernate, for at least two reasons:

1) You get Linq support (which requires something like an ORM)

2) NHibernate is very mature

There are no significant disadvantages. There are other options, but those other options have significant disadvantages.

I wrote some more on this a while ago:

.NET and ORM - Decisions, decisions

Michael Maddox
+7  A: 

Since other ppl have listed advantages I will just list the disadvantages


Disadvantages

  1. Increased startup time due to metadata preparation ( not good for desktop like apps)
  2. Huge learning curve without orm background.
  3. Comparatively Hard to fine tune generated sql.
  4. Hard to get session management right if used in non-typical environments ( read non webapps )
  5. Not suited for apps without a clean domain object model ( no all apps in world dont need clean domain object models) .
  6. Have to jump through hoops if you have badly designed ( legacy ) db schema.
Surya
I highlight the immense learning-curve as the single biggest reason to not use NHibernate on a project. For all their drawbacks, tools like LINQ to Sql are significantly easier to get to working than NHibernate. That said, I'd much rather use NHibernate on a project than any other ORM thanks to its amazing flexibility.
Programming Hero
Just on point 3, NHibernate lets you run straight SQL or stored procedures and will help translate the results into your model for when you really need to worry about the SQL used.
Garry Shutler
@Garry N+1 selects and its cousin over eager fetching are bane of most of nhinbernate projects . Using straight sql would defeat the whole point of a ORM
Surya
@Programming Hero linq and nhibernate are not mutually exclusive. They solve two different problems . I think there is a linq implementation for nhibenate too.
Surya
@Surya - I would say that N+1 selects is a bane for any ORM that is not used properly .
sirrocco
Same for over eager fetching. Plus , I think 3, 4, 5, 6 will apply to all ORMs right ?
sirrocco
Linq2Sql is limited to SQL Server and also Microsoft ditched it in favour of EntityFramework which is about same in many aspects as NHibernate.
Ray
@sirrocco yeah you might be right . Some orm projects out there claimed to have "solved" n+1 select *problem* ..so i wont say its acommon problem across any orm.
Surya
Advantages/Disadvantages comparing to what? If to poor DataSet then it is one set of things, if comparing to other O/R-M tools then it is other.
Dmytrii Nagirniak
@Dmitriy OP asked for disadvantages of bringing in nhibernate to a project , what aspect of nhibernate causes it is irrelevant
Surya
I don't understand how point 5 is relevant. NHibernate allows you to work from a domain driven design down to the database (whereas most other ORMs do not), but it doesn't require domain driven design.
Michael Maddox
A: 

Disadvantages: NHibernate is not a Microsoft product and therefore will face some resistance from coworkers who haven't heard of it. Especially FOSS bigots. Configuring the mapping files and lazy/eager loading behavior can be time-consuming. If your database has a bizarre naming convention, atypical design or very strict performance requirements, more work may be required than expected.

I say this a lot but ActiveRecord is a great layer over NHibernate. It uses attributes to map the data points to class members right in the classes themselves. People are not using this thing enough.

Chris McCall
+2  A: 

Advantages:

  • Open source
  • Based on widely approved patterns
  • NH is not code-generator :)

Disadvantages:

  • Half-done LINQ support
  • Low performance

(see for example performance and LINQ tests on ormbattle.net)

Alex Kofman
Speaking technicaly, NH IS code generator - it reads metadata and generates proxy classes using either LinFu or Castle DynamicProxy.LINQ support in Linq2Nhibernate seem to be full as far as Criteria API allows to go, so yeah it more limited than HQL but still very powerful and enough for most situations.Totally disagree about performance.
Ray
Agree with you, it's runtime code generator, but not design time. I personally don't like tools, that generate tons of code in design time. So it's an advantage of NH. As for LINQ, there is no doubt, they are just at the beginning of the road.
Alex Kofman
Those benchmarks are incorrect. See here http://ayende.com/Blog/archive/2009/08/15/benchmarks-are-useless-yes-again.aspx
Ray
Benchmarks are correct, Oren believes that ORM vendors shouldn't minimize overhead, added by ORM. In addition he wrote about batching CUD only, what about other operations? I agree that performance is not the main factor, but in some cases it is very important.
Alex Kofman
Benchmarks are correct, but NH team does not want to play the game honestly. Their own test vs EF they made was simply a hack: http://ormbattle.net/index.php/blog/105-a-honest-comparison-of-entity-framework-and-nhibernate.html
Alex Yakunin
+3  A: 

Advantages:

  1. Flexible and very powerful mapping capabilities.
  2. Caching.
  3. Very polished UnitOfWork implementation.
  4. Future query (article).
  5. Model classes are POCO - which effectively means you can easily implement anemic domain antipatter.
  6. Interceptors - you can do a kind of aspect oriented programming... Like very easily implementing audition, logging, authorization, validation, ect for your domain.
  7. Lucene.NET and NHibernate are well integrated with each other - gives you a very fast and effective implementation of full-text indexing.
  8. It's very mature and popular in enterprise environment.
  9. Big community.

Disadvantages:

  1. Already mentioned learning curve. You can start using NHibernate very fast but it will take you months to master it. I'd highly recomend to read Manning NHibernate book.

  2. Writing XML mapping can be very tedious especially for big databases with hundreds and thousands of tables and views and stored procedures. Yes, there is tools that will help you by generating those mappings but you still will have to do quite a lot of manual work there. Fluent NHibernate seem to simplify this process by getting rid of XML mappings, so is Castle ActiveRecord (AR though is impossible to use for anemic domain as you define mappings in attributes on your model classes).

  3. Performance may be low for certain scenarious. For instance large bulk operations. For those you might have to use IStatelessSession but its awkward experience, least to say...

Ray
For generating XML mappings, at least consider Active Writer: http://using.castleproject.org/display/Contrib/ActiveWriter
Michael Maddox