January 28, 2009

NHibernate

Lately I've been working on a new data access layer. The new project is for some C# code that I work on, so we decided to use NHibernate. I've been doing some research and playing with NHibernate. First thing is that the actual NHibernate site is not the best place to go for up to date documentation. I'm using NHibernate 2.0, but most of the documentation is specific to 1.0, and doesn't always work in 2.0. After some searching (more than was necessary, since I would expect something like this to be easy to find), I found another site: NHibernate Forge. It has content that is more updated, especially the main reference documenation.

So far everything seems to work pretty well. A few little things I have stumbled on:

NHibernate has great logging, but it can be overkill as times. I decided to limit the log4net appenders, so that the NHibernate logs didn't overwhelm the rest of the logging. Since I use multiple different apps that all use the same configuration for log4net, I setup the logging programatically, instead of trying to maintain identical logging config files for each. This can make it a little harder to set up loggers, but I stumbled on how to do this as well.

I was also trying to look into NHibernate transactions, and the possibility of nested transactions using savepoints. I use these in some of my other code to deal with certain error cases where some things need to be rolled back, but not an entire transaction. It seems that this doesn't really work well with NHibernate. Because of the tight coupling between the data objects in memory and the database, rolling back part of a transaction could leave the in memory objects in an inconsistent state. Fortunately in most cases this will be OK, but it is nice to be able to use the savepoints in some cases.

I think I've worked out most of the details for what I need, I'm just trying to create some lightweight wrappers around NHibernate, to give me an abstraction layer between NHibernate and my code. Hopefully everything will be smooth sailing from here on out.

No comments: