NHibernate – Generation, Attributes, Nullables, and Generics

Over the past few months I’ve been working on a little generator which will generate a thin layer (classes, data access objects, configuration etc) that will sit over NHibernate from a database schema and allow you to work against a particular database. Its pretty simple at the moment – Just point and click go. As I’ve learnt a bit more about it and found out some of the little tricks here and there I’ve gradually extended the template to a point where it is pretty solid (as always theres still some ironing out to do). The things I’ve been integrating into the generation template so far have been:

1) Using classes decorated with attributes (this attribute package is found in the NHibernate Contrib package) rather than the xml mapping files. I decided that the xml mapping files where just something extra to look after in the solution – I thought the less the better. The attributes on the other hand do make these base classes a bit messy but I figured I should never have to look at the generated code – The classes should just be there to use. Although generating the mapping files from the database/inserting attributes like this is really chucking out a whole heap of functionality that NHibernate will allow, I figured I could go back at a later time and extend the generator to provide a richer model. At the moment I just purely want a single class to table mapping and it should just all work.

2) Adding support for Nullable types (I’m using a dll I found called NHibernate.Nullables2.dll – not the one in the NHibernate Contrib package. This can be found here: http://dotavery.com/blog/archive/2005/09/30/5202.aspx). This is a very cool package as it allows us to use the new .Net 2.0 nullable type notation eg. bool? _flag; in our code/base classes but in the attribute or mapping fies declare it as a special type like: NullableBooleanType 😀 Whats cool here is that from our program side of things these types are transparent while from the NHibernate side of things it understands what we are talking about. And using it is so easy – just reference the dll and change the type in the mapping file :-D. Here’s at example of using NHibernate.Nullables2 with the attribute package:

[NHMA.Property( Name = “RiderNumber”,Column = “RiderNumber”,NotNull = false,TypeType = typeof(NullableInt32Type) )]public int? RiderNumber{get { return _riderNumber; }set { _riderNumber = value; }}

 On the other hand when you use the Nullable package from the contrib you have to use the special types throughout your program code – not very nice 😦

3) Adding in the support for generic collections. This has been what I’ve been working on for the last week or so after work (when I can), and after playing around a bit I have it working – not as nicely as I would have liked but working none the less. The package I used to provide support for generics can be found here: http://www.ayende.com/projects/nhibernate-query-analyzer/downloads.aspx. (this is a cool blog too – add it to your feeds if you have one 🙂 ).

The reason putting generics in there was so important for me was that I’m working in .Net 2.0 and then suddenly with NHibernate only under 1.1. I had to go back to using IList – as the developer you have to remember what is in that collection when you come to use it. Generics on the otherhand provide a strongly typed collection IList of T so when you come to use it you know exactly what it is! No casting exceptions for me :-). To get this to work is not the easiest (not hard – but more a case of looking at an example and then just copying it) and then fixing the errors that come up when you run the app up. I’ll give an example of these bits and pieces in another post since this ones getting pretty long …

4) Working on adding enumeration suppport to the generation process – this is working but I’ll keep you posted once I have tested it and used it a couple of times in some app’s I’m working on at the moment 🙂

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


%d bloggers like this: