This is not a simple way to make NHibe convert 0 to null and vice-versa, but an alternative suggestion (maybe or maybe not right in your situation).
Imo, using a default value to mean null is an antipattern; maybe you agree. I take it that you're trying to hook up some legacy code with a proper database; is that correct?
If you are in control of the business class which you are mapping (class Foo), then I recommend exposing a nullable version of SomeProperty for use moving forward:
public class Foo
{
/// for new code, and for mapping to the database:
public int? SomeProperty_new {get;set;}
/// for legacy code only. Eventually, refactor legacy code to use SomeProperty_new instead, and just remove this needless property.
[Obsolete("This uses default value to mean null. Use SomeProperty_new instead.")]
public int SomeProperty_old
{
get
{
if (SomeProperty_new == null)
return 0;
else
return SomeProperty_new;
}
set { /* convert from 0 to null if necessary and set to SomeProperty_new.*/ }
}
}
You'll want better names than SomeProperty_new and SomeProperty_old, though.
On the other hand, if 0 is never a valid value (except to mean null), you could instead make the DB use a non-nullable value. It really depends on the situation at hand.