CodeBetter.Com
CodeBetter.Com
RSS 2.0 via Feedburner
           Do you Twitter? Follow us @CodeBetter

Jeremy D. Miller -- The Shade Tree Developer

Under the hood and working with .Net, TDD, Software Design, and Agile Stuff

Mapping Enumerations with NHibernate - and hooray for open source unit tests

This is just to make a permanent note to myself.

We were using NHibernate this morning to map a class that had an enumeration type property.  By default NHibernate will store the enumeration integer value in the database.  Since this particular table will be viewed directly by support we wanted to persist the enumeration string name.  Here's how you do it (example is taken from NHibernate itself):

Create your class that has an enumeration property:

	public class EnumStringClass
	{
		private int _id;
		private SampleEnum _enumValue;

		public EnumStringClass()
		{
		}

		public int Id
		{
			get { return _id; }
			set { _id = value; }
		}

		public SampleEnum EnumValue
		{
			get { return _enumValue; }
			set { _enumValue = value; }
		}
	}

	public enum SampleEnum 
	{
		On,
		Off,
		Dimmed
	}

Next, you need to create a subclass of the NHibernate NHibernate.Type.EnumStringType class for your custom enumeration type.

	public class SampleEnumType : NHibernate.Type.EnumStringType
	{
		public SampleEnumType() 
			: base( typeof( SampleEnum ), 10 )
		{
			
		}
	}

In the NHibernate mapping you need to override the type mapping to use your new derived EnumStringType subclass. The subclass will handle the coercion from strings to enumeration.

		<property type="NHibernate.Test.TypesTest.SampleEnumType, NHibernate.Test" name="EnumValue" column="enumc"></property>

Unit Tests are Documentation

There is an important underlying point here.  The NHibernate documentation isn't all that you'd hope for and Google didn't really help much either.  It didn't matter because as soon as I fired up the NHibernate code I quickly found the relevant unit test that demonstrated exactly what to do.  Intention revealing unit tests are one of the best forms of technical documentation, and by definition, they can't get out of synch with the code.  One of the best things you can do to enable the people that follow you in the code is to make your unit tests as clear as possible in the specification and usage of the code.



Comments

sotto said:

> ...Since this particular table will be viewed directly by support...

Is there a reason why you wouldn't create a View that can be used to query that specific table?
# August 4, 2006 4:28 PM

Jeremy D. Miller said:

Sotto,

Not at all, I just hate to have lookup tables cluttering up the database.  I'd rather have the human readable values for myself for that matter.

Jeremy
# August 5, 2006 5:02 PM

Daniel said:

Thanks, another reason why both hibernate nhibernate rocks!

# September 13, 2006 1:07 PM

Unit Tests as documentation « Jonne Kats said:

Pingback from  Unit Tests as documentation &laquo; Jonne Kats

# October 27, 2007 7:21 AM

NHibernate Mapping an Enum « MindTrace said:

Pingback from  NHibernate Mapping an Enum &laquo; MindTrace

# April 17, 2008 9:23 PM

Leave a Comment

(required)  
(optional)
(required)  

Enter the numbers above:
Add

About Jeremy D. Miller

Jeremy began his IT career writing "Shadow IT" applications to automate his engineering documentation, then wandered into software development because it looked like more fun. Jeremy previously worked as a systems architect building mission critical supply chain software for a Fortune 100 company and learned agile development practices as a .Net consultant at ThoughtWorks, one of the pioneers of agile development. Jeremy is the author of the open source StructureMap (http://structuremap.sourceforge.net) tool for Dependency Injection with .Net and the forthcoming StoryTeller (http://storyteller.tigris.org) tool for supercharged FIT testing in .Net. Jeremy's thoughts on just about everything software related can be found on his weblog "The Shade Tree Developer" at http://codebetter.com/blogs/jeremy.miller, part of the popular CodeBetter site. Jeremy is a Microsoft MVP for C#. Check out Devlicio.us!

This Blog

Syndication

News

All opinions expressed here constitute my (Jeremy D. Miller's) personal opinion, and do not necessarily represent the opinion of any other organization or person, including (but not limited to) my fellow employees, my employer, its clients or their agents.

About Me

"Best Of" Compendium

StructureMap (Dependency Injection for .Net)

StoryTeller (Supercharged Fit)

Build your own Cab

TestDriven

MVP