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

Jeffrey Palermo (.com)

Blog moved to www.jeffreypalermo.com

NHibernate: Casting is a state change and makes a persistent object dirty immediately - level 200

NHibernate knows when an object under its watch has changed.  As soon as the object changes, it is "dirty".  Some other changes might cause an object to be dirty as well.  One that my team recently encountered is a cast.  We use an enum of type byte.  It's only a few items (less than 255), so we use a tinyint in our database.  When our mapping uses type="byte", NHibernate casts from the byte to our Enum type when hydrating the object.  This cast is a change because when NHibernate checks the value, it's an Enum, not a byte. 

To get around this cast (implicit or not), we use the fully qualified type name of the Enum in the mapping.  NHibernate understands Enums natively, so just put in the enum type, and you are off to the races.  Note that if you are using an Enum that is nested inside a public class, you need to follow .Net's rules for fully-qualified type names.

See MSDN's documentation for this: 

http://msdn2.microsoft.com/en-us/library/w3f99sx1.aspx

TopNamespace.SubNameSpace.ContainingClass+NestedEnum,MyAssembly


Comments

Ayende Rahien said:

You can just drop the type name completely, NHibernate will do the Right Thing here. Actually, this is the first time I hear about this issue, because I always leave the type name when it is obvious from the classes.
# October 23, 2006 2:51 PM

Jeffrey Palermo said:

I've tried it both ways, and when going from "byte" to my C# enum, my object is dirty right away. Specifying the enum in the mapping gives me the correct behavior. I'll admin I didn't try it with a normal Enum that defaults to the "Int32" data type.
# October 23, 2006 3:23 PM

About Jeffrey Palermo

Jeffrey Palermo is a software management consultant and the CTO of Headspring Systems in Austin, TX. Jeffrey specializes in Agile coaching and helps companies double the productivity of software teams. Jeffrey is an MCSD.Net , Microsoft MVP, Certified Scrummaster, Austin .Net User Group leader, AgileAustin board member, INETA speaker, INETA Membership Mentor, Christian, husband, father, motorcyclist, Eagle Scout, U.S. Army Veteran, and Texas A&M University graduate. Check out Devlicio.us!

This Blog

Syndication

News

Headspring Systems

View Jeffrey Palermo's profile on LinkedIn

See my new blog at .jeffreypalermo.com