views:

30

answers:

1

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).

+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
I would do this with a user type too.
Stefan Steinegger
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
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
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
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
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