NHibernate

September 7th, 2005
Posted by gerrod in: technical

I’ve been playing around with NHibernate for the last few days, as it’s a product I’ve been curious about for some time. If the name sounds familiar, it should – it follows in the tradition of NAnt, NUnit and others being a port from an existing Java product.

So what is it? Basically (very basically!) it’s an Object-Relational mapper – providing you with “automatic” translation between your domain objects and your database tables. Of course, it’s much more than that – but lets keep it simple for the moment. OR mappers aren’t a new concept – they’ve been around for ages in fact – but recently it seems like they’re a dime a dozen. Maybe it’s just that I’ve only recently taken notice of how many there are. But whatever the reason, they’re definitely a good thing – anything that saves me writing a whole bunch of data access code and stored procedures is definitely a good thing.

Anyway, here’s my experience with NHibernate so far. Remember that it’s still in beta (last release is 0.9.1) so it still has a little way to go before being a fully fledged release.

Pros:

  • It’s fast. Real fast. My experience thus far is that there is no noticable difference in performance between using NHibernate compared to traditional stored procedures and a data access layer. (Disclaimer: I haven’t done any heavy-duty load testing, this is more of an observation.)
  • It’s super flexible. Not only does it support a whole bunch of databases that I’ve never even heard of, but it has it’s own built-in query language (HQL) which is very powerful and fairly straight-forward to learn (it’s designed to mimic SQL).
  • It’s based on Hibernate, which has been in development for ages. This basically means it has a huge leg-up on other OR mappers which are written from scratch.

Cons:

  • Documentation is scratchy at best. This will no doubt improve with time, and as the product comes closer to release – but for the moment, trying to piece together documentation is often a nightmare. Sure, you can reference the Hibernate documentation as it is mostly the same, but not all the features from Hibernate have been implemented in NHibernate yet (and gosh darn, if it isn’t the feature that you’re trying to use right now…)
  • It’s not easy to learn. Especially in the realms of ASP.NET programming. For example, there’s no “best practice” to recommend the best way of handling NHibernate sessions over a request. Sure, there’s lots of debate and examples from various ways that people have done it – but for a n00b, that just makes it difficult to work out which way to go. (FWIW: I went with session-per-request, opened by an AbstractService and closed using a HttpModule (with some tweaking)).
  • And the kicker – it doesn’t support outer joins against an access database. Given that the code is open-source though, I’m doing my best to fix this one.

Overall, I’d highly recommend NHibernate, and it is quickly becoming my object-mapper of choice. But be warned; the learning curve is steep, and often very frustrating! I still have a way to go, but at least that “light at the end of the tunnel” is starting to get a bit brighter now.

3 comments

#1 Ben September 7th, 2005 at 2:59 pm

When does gerrod.com become open-source?

#2 kristy September 7th, 2005 at 7:05 pm

You’re right, Gerrod. This article is fascinating! I’m so glad you ‘kept it simple’. :-P

#3 Ian September 8th, 2005 at 2:21 pm

Better than Wilson?