I am working with a legacy database that stores "blank" values as a single space. Is there a way with Fluent NHibernate, either by way of a convention or mapping override, that I can translate this "blank" value as a .NET null? (I know that I will need to save nulls to this database as a single space still but I will use an save event listener for that).
views:
30answers:
1
+1
Q:
how do you translate a space value in a database field to a null in .NET with Fluent NHibernate?
+2
A:
You should use a custom implementation of IUserType
for this, not a listener. Here's one example of many examples available on the web. James Gregory wrote a good article on how to implement IUserTypes using a Fluent NHibernate conventions.
Jamie Ide
2010-03-30 12:27:25
I would do this with a user type too.
Stefan Steinegger
2010-03-30 12:28:16
isn't there any kind of convention or something that i can set instead of having to set IsCustomType on every field (this is a legacy convention throughout this db)?
gabe
2010-03-31 21:22:30
Yes. I'm not an expert on conventions but you can substitute an IUserType for another type with conventions. In your case you could replace all string type mappings with the IUserType that stores empty strings as a single space. This should help: http://jagregory.com/writings/fluent-nhibernate-auto-mapping-type-conventions/
Jamie Ide
2010-03-31 22:23:42
great! thx! the only problem is that i don't have the newest version of nhibernate and i'm hesitant to upgrade right now because i'm using sharp architecture which doesn't have itypeconvention and doesn't seem to have addtypeconvention either.
gabe
2010-04-01 14:14:32
I'm not all that familiar with Sharp but I know it uses/can use Fluent NHibernate and that's where the convention magic lies.
Jamie Ide
2010-04-01 17:47:15
right, what i'm saying is that the Sharp i am using is dependent on a particular version of Fluent, which doesn't have the itypeconvention.
gabe
2010-04-04 22:43:16