tags:

views:

113

answers:

3

Hi,

For a new project we are looking at NHibernate. We like it a lot overall, but one thing bothers us: it seems to be very resource consuming. Apparently NHibernate will load all the properties of an object even if you need only one of the properties. More over, it will do the same for the properties of child objects.

So we are weighing the pros and cons of NHibernate. What would you say they are, and if the upsides make up for the downsides?

+2  A: 

Advantages:

  1. Second Level Caching
  2. Creates the objects representing your data natively
  3. Creates the SQL queries and avoids SQL injection
  4. Lazy loading
  5. Following foreign keys are easier.
  6. DB translation (change the dialect)
  7. The framework is well supported and is opensource
  8. There are lots of tools that work with and generate code/mapping files for Hibernate

Disadvantages:

  1. Can be slower than direct querying
  2. Object initialization is slower than by hand
  3. Initial configuration is a pain
  4. Mapping is checked at runtime and that can be a pain to setup

It depends what you are doing. Hibernate is not the tool for bulk updating.

monksy
+2  A: 

Disadvantages:

  1. It can take a while to get the hang of editing HBM files (although you could use Castle's ActiveRecord, which does session management, and let's you declare relationships with attributes, which it uses to generate HBM, since NH is underneath AR. Note: you don't have to use the AR pattern with Castle's AR).
  2. It's probably going to run more queries to retrieve a particular graph of data than you would if you wrote it by hand
  3. It's more difficult to make use of the power of the DB engine, since NH treats is like a dumb record store
  4. Oracle support is not as good as other dialects.

Advantages

  1. You can use Linq-to-NH, and use linq style queries against all the supported dialects.
  2. You can use HQL instead of SQL
  3. You can switch DBs venders with a couple lines in a config file
  4. There are tools to generate your schema for you.
  5. Support for versioning of instances
Josh Pearce
+1  A: 

Here are some things that haven't been mentioned yet (sorry, it's not in pro/con format). These apply to situations where you will be creating a generic framework to do basic entity operations (so you have a reusable NHibernate library that you can use for other projects).

  • Setting it up is a real pain. Ours is stable now, but we started developing it a year ago.
  • Consider the type of applications you'll be developing (i.e., web forms or WinForms), because the session model you use in the generic framework may be different; or, you might want to develop a framework where you'd want to be able to plug in your own session management implementation. There are a lot of choices here.
  • If there is a chance of developing a highly concurrent application using the framework, design the concurrency model right from the start -- it could be very difficult to get it working properly later on.
  • If you use the NHibernate.Mapping.Attributes library, you don't have to deal with external XML mapping files. All you do is add metadata to your data object classes. This is really handy, intuitive, and easy to maintain.

That's all I've got for now. If I think of anything else, I'll add to my list.

Jon Seigel