Thoughts On Validating NHibernate Mapping Files

Ben brought up a good (pain) point in his comment on my last post that can definitely occur when doing integration testing using NHibernate.  Invalid mapping files can cause the ISessionFactory to break upon creation, often leaving you to check the logs to see what’s going on.  But there are a few things you can do to help prevent this, or at least, make it easier to figure out what’s wrong.

XML Intellisense Is Your Friend

If you haven’t already, you’ll want to enable intellisense for your NHibernate xml mapping files.  This is a good first step to make sure you’re xml mapping files are valid according to NHibernate’s mapping file schema.  Any syntax errors that violate the schema will immediately be apparent.

nh_mapping_missingkey

Doh!  I forgot to add the <key /> element.  No worries…  :)

nh_mapping_intellisense

So xml schema validation and intellisense are kind of your first line of defense against making mistakes in your mapping files.

Don’t Want To Hand Write XML Mapping Files?

For the most part, I still do hand-write my xml mapping files, mostly because they’re pretty easy once you write a few, and with intellisense enabled, as shown in the previous section, it’s even easier.  But, there is (at least) one alternative to this.  One of Ayende’s great NHibernate utilities is his NHibernate Query Analyzer (NHQA).  This has a couple of uses (another of which I’ll get into later), but included in it is a UI for maintaining NHibernate mapping files.

nhqa_mapping_ui

So this is one more way you can make sure you don’t have errors in your mapping files because using the UI will make sure your mappings are in the correct format.

Are The Properties In My Mapping File Valid?

Schema validation and a configuration UI are great for making sure your mapping file is in the correct format.  But one thing it won’t catch, for instance, is a <property /> mapping that points to a non-existing property on the specified class.  One quick and dirty way to catch these is to load up your mapping files into the NHQA and “build the project”. 

nhqa_main_ui

When you click on Build Project, if any mapping errors are detected, it’ll show an exception dialog with a pretty good error message telling what’s wrong.

nhqa_mapping_exception

Of course this is still a manual process, which is not good, but it’ll get you by for a quick sanity check.  It’s also just a great tool in general for learning and building HQL queries against a real database.

(On a side note…  Is it just me, or does it seem that most open source tools have much better, detailed error/exception messages and logging than a lot of the commerical tools?  I’ve always been impressed with NHibernate and Castle in that regard…)

Anyways, hope you can find this useful.

Related Articles:

Post Footer automatically generated by Add Post Footer Plugin for wordpress.

This entry was posted in nhibernate, tools. Bookmark the permalink. Follow any comments here with the RSS feed for this post.

Comments are closed.