NHibernate Built-in Code Based Mapping instead of using Fluent NHibernate
This is just a quick post to show how to use the new NHibernate built-in mapping instead of using Fluent NHibernate. It looks like the Fluent Hibernate is not compatible with the latest version of NHibernate that is available in NuGet. Although I am a fan of Fluent NHibernate but I am not sure about it’s future as the core NHibernate now has this similar kind of code based mapping instead of those boring xml files. I will show you the Fluent NHibernate Mapping first then the new mapping. This is extracted from one of my pet project that I am using to play with jQuery Mobile and Spine. Lets say I have a basic User object which was mapped in Fluent NHibernate like the following:
Lets take another example, this time the Task object:
is changed to:
Now, the most important thing building the NH SessionFactory, please note that in the above, the core NHibernate mapping the IClassMap interface does not belongs to the NHibernate, it is just a custom interface by me to use separate classes for each object mapping (If there is any built-in class in NHibernate the does the same thing, please free to post it in the comment box below). Here is the code that builds the SessionFactory.
There are two interesting things in the above first I am using two ModelMapper events to add my own convention, first all of table primary key generator should be the GuidComb and next based up the type I am setting up whether the column should support null or not, in Fluent NHibernate this support was available through some interfaces and lastly there check for the type of database, in development and test mode I am using SQLCompact edition and for production SQL Server.
That’s it for tonight, maybe some of the non NHibernate Mafia like me find it useful.
[Update: Hazzik is correct the Fluent NHibernate that is available in NuGet is fully compatible with the latest NHibernate.]
[Update 2: Check the gist of Sergueev in the comments section, how you can use the built-in class of NHibernate instead of creating custom interface like I did for the mapping.]