Tuesday, May 08, 2007

What's so good about NHibernate?

I work with data quite a bit and am always a sucker for on the lookout for new ORM/code generation tools to help automate cruds and queries. For a while now it has been MyGeneration d00dads. Free (always a good thing, since I work for a non-profit), fairly shallow learning curve, not too many quirks. But you never know when something better might be waiting for you Out There.

I have seen several items lately, particularly from Ayende of course (I love the idea of Bumbler!) Sam considers it Noteable. In the past, he has also mentioned it in contrast to Microsoft's flagging ORM efforts as a Good Thing.

Time to check it out. I go to NHibernate.org (or rather a page off of Hibernate.org) and check out the Nhibernate Quick Start Guide. In five steps, the first of which you may not need (create a database), the last of which is really several steps to get the configuration and connection objects lashed up, you can begin firing at your database.

The first few examples look good, such as:
User newUser = new User();
newUser.Id = "joe_cool";
newUser.UserName = "Joseph Cool";
newUser.Password = "abc123";
newUser.EmailAddress = "joe@cool.com";
newUser.LastLogon = DateTime.Now;
session.Save(newUser); // commit all changes to the DB

Seems natural enough. Then we go down a bit to find

Let's say you want to retrieve an object when you know the user ID (eg. During a login process to your site). Once a session is opened it's a one-liner; pass in the key and you're done:

// open another session to get the new user
session = factory.OpenSession();
User joeCool = (User)session.Load(typeof(User), "joe_cool");

Casting? Why are we casting?? Why not create an object of the type you want and get IntelliSense for its columns, properties, and methods? That's what the MyGeneration d00dads architecture lets you do. Here is their canonical example.

And what's up with all the code you have to write? One would assume that NH devotees use code generation for the class with its properties and the XML mapping file that has to be embedded in your assembly. But no mention of it in the quickstart. If I thought I would have to do all that, I would give up right away. (As it happens, MyGeneration has a template that does the class and .hbm.xml file. If it didn't, I would surely be writing my own.)

Now a lot of any ORM or DAL tool is what you get used to doing. If NH floats your boat, I won't try to argue you out of it. But I'd love to know if anyone using NH has also tried something a bit more automated, perhaps even SubSonic, which is starting to look very nice in v2.0.

If you have tried something else and still use NH, I'd love to know why.

1 comment:

kevin said...

2 years in between posts? I guess a welcome back is deserved.

I too gave up on NH because of all the plumbing you need to do. I'd assume once you become an expert at it like Ayende and the others it becomes a joy to work with - they have to be believers for some reason, right?

SubSonic was a blessing when I came across it. And yes, v2.0 is a great upgrade.

For now, SS does what I need. And I assume both the forthcoming LINQ and Entity Framework will dance between what NH and SubSonic offer.