Jeremy D. Miller -- The Shade Tree Developer

Sponsors

The Lounge

Wicked Cool Jobs

Syndication

News

Advertisement

Images in this post missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at imagehelp@codebetter.com
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.


Posted Mon, Feb 20 2006 12:49 PM by Jeremy D. Miller

[Advertisement]

Comments

sotto wrote re: Mapping Enumerations with NHibernate - and hooray for open source unit tests
on Fri, Aug 4 2006 4:28 PM
> ...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?
Jeremy D. Miller wrote re: Mapping Enumerations with NHibernate - and hooray for open source unit tests
on Sat, Aug 5 2006 5:02 PM
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
NHibernate Part 4: Mapping techniques for aggregation - One-To-Many mapping « Hungry for Knowledge wrote NHibernate Part 4: Mapping techniques for aggregation - One-To-Many mapping &laquo; Hungry for Knowledge
on Mon, Sep 4 2006 2:05 PM
Daniel wrote re: Mapping Enumerations with NHibernate - and hooray for open source unit tests
on Wed, Sep 13 2006 1:07 PM

Thanks, another reason why both hibernate nhibernate rocks!

Unit Tests as documentation « Jonne Kats wrote Unit Tests as documentation &laquo; Jonne Kats
on Sat, Oct 27 2007 7:21 AM

Pingback from  Unit Tests as documentation &laquo; Jonne Kats

NHibernate Mapping an Enum « MindTrace wrote NHibernate Mapping an Enum &laquo; MindTrace
on Thu, Apr 17 2008 9:23 PM

Pingback from  NHibernate Mapping an Enum &laquo; MindTrace

Emad wrote re: Mapping Enumerations with NHibernate - and hooray for open source unit tests
on Mon, Aug 18 2008 12:26 PM

Too late comment, but what about Globalization;

if you have enumeration that need to be displayed in different languages?

Emad wrote re: Mapping Enumerations with NHibernate - and hooray for open source unit tests
on Mon, Aug 18 2008 12:26 PM

Too late comment, but what about Globalization;

if you have enumeration that need to be displayed in different languages?

Mapping Enumeration of type int in NHibernate « Emad Al-Ashi’s Blog wrote Mapping Enumeration of type int in NHibernate &laquo; Emad Al-Ashi&#8217;s Blog
on Thu, Oct 2 2008 5:34 PM

Pingback from  Mapping Enumeration of type int in NHibernate  &laquo; Emad Al-Ashi&#8217;s Blog

James L wrote re: Mapping Enumerations with NHibernate - and hooray for open source unit tests
on Mon, Jan 5 2009 8:25 AM

This comes quite high up the google search results, but there's now an easier way using generics.

Instead of creating your own type, just use:

<property

type="NHibernate.Type.EnumStringType`1[[SampleEnum

, SampleAssemblyName]], NHibernate"

name="EnumValue" column="enumc" />

Mapping Enums to custom strings in NHibernate - Software Warlock wrote Mapping Enums to custom strings in NHibernate - Software Warlock
on Fri, Jul 3 2009 12:04 AM

Pingback from  Mapping Enums to custom strings in NHibernate - Software Warlock

Cameron McKay » How to put spaces in your NHibernate enums wrote Cameron McKay &raquo; How to put spaces in your NHibernate enums
on Sun, Jul 12 2009 10:33 PM

Pingback from  Cameron McKay » How to put spaces in your NHibernate enums

Add a Comment

(required)  
(optional)
(required)  
Remember Me?
Devlicio.us