I have a custom user type that is used to map from a decimal value out of the database to a property on an entity using Fluent NHibernate. The object works correctly as does the mapping to the object but I do not know how to dynamically change the precision and scale of the decimal sql data type so that I can use different precision and scale in different class maps.
Here is an example of my custom type;
public struct MyCustomType
{
private readonly decimal value;
public MyCustomType(decimal value)
{
this.value = value;
}
public static implicit operator MyCustomType(decimal value)
{
return new MyCustomType(value);
}
public static implicit operator decimal(MyCustomType value)
{
return value.value;
}
public string ToString(string format, IFormatProvider provider)
{
return value.ToString(format, provider);
}
}
And here is how I am doing the user type.
public class MyCustomUserType : IUserType
{
...
public object NullSafeGet(IDataReader rs, string[] names, object owner)
{
MyCustomType customType = (decimal)rs[names[0]];
return customType;
}
public void NullSafeSet(IDbCommand cmd, object value, int index)
{
var parameter = (IDataParameter)cmd.Parameters[index];
parameter.Value = (decimal)(MyCustomType)value;
}
...
public SqlType[] SqlTypes
{
//I think I could hard code the precision and scale here
get { return new[] { SqlTypeFactory.Decimal }; }
}
public Type ReturnedType
{
get { return typeof(MyCustomType); }
}
public bool IsMutable
{
get { return true; }
}
}
And finally this is how I am mapping the object
public class SomeObjectMappingBase: ClassMap<SomeObject>
{
protected SomeObjectMappingBase()
{
//Currently I do this
Map(x => x.CustomTypeField).CustomType<MyCustomUserType>();
//I would like to be able to do this but it does not work
Map(x => x.CustomTypeField).CustomType<MyCustomUserType>().Scale(10).Precision(20);
}
}
you can implement IParameterizedUserType or subclass MyCustomUserType for each combination of precision and scale.
Update: unfortunatly i can’t think of a way to enable the syntax
.CustomType<SomeObject>().Scale(x).Precision(y). insteadoption 1 to subclass
option 2 parameterizedType
option 3 get somehow this to work: dont map the custom type but set it on the config