Wednesday, November 02, 2005

Generics and nullables in NHibernate

This is one of the more frequently requested features for NHibernate
Rightly so, for those who are using .NET 2.0.  Once you have
generics and nullable types, it is hard to do without them.  NHibernate Contrib
is the more open project that adds in features that are not stable or
supported well enough to be put into NHibernate itself.  It has
some classes to help with nullable types, but they are not .NET 2.0
nullables, and no generics support exists.


That's why I wrote MyNHibernateContrib
I'm afraid documentation is poor, but I have some unit tests, and I'm
hoping to add better documentation and example code soon. 
Meanwhile, I have a forum set up where users can post questions and
I'll do my best to answer them.  I use MyNHibernateContrib in all
my projects to enable me to use .NET
2.0 generics and nullables.  It does the job quite nicely.  I
hope others find it useful.  It's licensed LGPL.



I use it to solve one other problem popular in the Hibernate world:
parent-child relationships where one of the relationships has to be
inverse='true'.  See, when you establish the parent-child
relationship, you add the child to the parent's collection, and you set
the child's parent reference to the parent.  It's two steps to
create the relationship.  Conceptually, all you should have to do
(IMO) is one of those two and have the other done for you.  It
fits the business object model better (at least for me).  So I
wrote a few new collection classes that implement all the right
interfaces so you can use them anywhere you use normal
collections.  These collections automatically set the parent
reference for you when you add a child to the parent's
collection.  It takes a little extra code in the collection
definition, but writing code that accesses the objects is such a nicer
experience that for me it makes up for it well.  At least until
the NHibernate group comes up with a better solution.

No comments:

Post a Comment