June 01, 2009

nHibernate – To be or not ?

There are lots of chaos around ORM like nHibernate. Some of in favor of nHibernate where as some hate it like nothing. The triumph of nHibernate is totally depends on architecture of your project & complexity of the database.

Advantages of nHibernate

  • Rapid application development – you don’t have to write any SQL or stored procedure!
      • Mapping can be made easy with ActiveRecord (attribute-based mapping) - you can reduce the time of writing XML or any relational attributes.
  • Better for maintenance – no stored proc, no SQL therefore any table changes affects only entities & associated layers
  • Clear separation of UI & DB layer. If you rename any field in any table then you have to change only entity mapping. Where as in traditional relational model any table field change affects all layers.
  • Easily migrate your code between different databases
  • Provides business entity form of representation of DB tables so you get all advantages of OOP.
  • Massive gain in performance with distributed cache (e.g. memcache) (assuming most data is non-volatile)
  • Think about nested query

Disadvantages

    ORM is not a good choice if
  • your database has complex(deep) relationships
  • high frequency applications where data is volatile
  • old/legacy/migrated database – In ORM first you have to develop object model & then database. If you don’t want to break existing poor relations then ORM is not a good choice
  • disconnected layers – smart client won’t supports lazy loading; transferring data & message size (Creating DAO classes mapped with domain may improve performance but requires lots of coding & therefore future maintenance)
  • consider time require to train developers
  • consider time for optimization

Conclusion

DAO and ORM are both valid approaches to persistence and the choice between them needs to be considered on a per-project basis. DAO approach is industry standard & proven approach. Since DAL is most vital part of any architecture you need to be VERY careful if you are going to use ORM like nHibernate.

Some refrences & sample code