The blog of Tobin

Tobins nerd blog on .NET, Software, Tech and Nice Shiny Gadgets.

Saturday, July 23, 2005

2 Pleasant NHibernate Experiences

I've been using NHibernate on a variety of projects over the last year or so, and it never fails to please...

Retrofitting NHibernate
I recently had a project that started out with simple ADO.NET/DataReader/DataSet based data access. After a while, I evolved to using objects for data-structures, and writing simple "loader" classes that took data out of the DB and mapped it into these objects. A few months later it turned out we needed to both read and write these objects to the DB, and at this point I realised my simple hand-rolled data access code had taken me as far as it could. I certainly don't need to re-invent the wheel when it comes to object relational mapping tools.

The interesting thing about this project was that it started out without any need or provision for an O/R mapper: my "data aware" classes didn't even store any DB identity fields because the app never needed them. I was worried that plugging in NHibernate into this existing code base would be tricky - mainly because I hadn't designed the application with that in mind. Fortunately it was remarkably simple - thanks to NHibnerates flexibility.

The grunt of the work was just setting up the mapping files for the several classes that needed reading/writing. Some tweaks to the classes were needed, such as identity fields had to be added. I think the whole process took about a day, and it really was a relatively straight forward experience. It just goes to show you can start simple and then retrofit an O/R mapper if and when you feel any data access pains!

Switching RDBMS from SqlServer to MySql
Roughly 6 months into the project we decided to switch databases from SqlServer to MySql. I'm not knocking SqlServer, it's fantastic, but we basically needed to cut costs, and MySql offered all the features we needed.

When someone comes along and says "can we switch databases", it's can be quite a considerable challenge. However, since we were using NHibernate, all the hard work had been done for us. All I had to do was enter a new connection string and tweak some configuration setttings, and "ping", everything was working against our MySql database. I do recall having to tweak one of the "forumla" fields in the hbm.xml mapping files, but that was it. Unfortunately the app also used some "raw" SQL in places - bypassing NHibnerate, and converting this ADO code + SQL proved to be much more time consuming 8-(

Anyway, there's just a few of my recent but pleasant NHibernate experiences. I'm using version 8.0.4 now, and the dev team are making great progress I think. Good work!

0 Comments:

Post a Comment

<< Home