How flexible is NHibernate configuration

NHibernate is really a flexible product, and the configuration capabilities make no exception. In the example we used for NHDay, Guardian  used ConfORM to configure NHibernate without XML mapping file, everything is good, but I encounter a little problem.

One of the examples shows how you can create Dto object directly from HQL Query:


Figure 1: A simple HQL to create a DTO directly from the query.

This test fails because AdultDtoSample is not known by nhibernate (Figure 2)


Figure 2: The error generated by NHibernate because it does not know the AdultDtoSample class specified in the query

The solution is really simple, just add an import section inside a mapping file… but … wait … with ConfORM you have no mappings, but how can I specify import directive to ConfORM? Before trying to look at ConfORM help, you should understand that NHibernate has a lot of way to specify configuration and you can mix them with no problem. ConfORM is useful to auto generate mappings with rules, but to add Import clause a simple standard Xml mapping is enough. Just to show how flexible NH is, I use a not so well known feature.. adding a xml file directly as a string.


Figure 3: Adding a mapping directly from code.

In Figure 3 I show how you can add a mapping as string, you can place this code after ConfORM has already configured mapping for domain classes, mixing the way you are actually configuring NH. If you look at Configuration object you find a lot of different techniques:_


Figure 4: Some of the various methods offered by Configuration Object to specify Configuration.

As you can see from Figure 4 you really have a lot of way to add configuration to NH and the good point is that you can mix all of them, using the most appropriate one for each of your need.