You could write a UserType
. The advantage is that you could easily distinguish different types of decimals (you most probably don't want to have the same precision for all decimals).
Property(
x => x.Dollars,
m => m.Type<MoneyUserType>()
);
You have some effort to put this into all monetary properties, but then you have a more readable and self descriptive mapping definition.
A similar solution (syntactically), but easier to implement, is to write an extension method which sets up the precision.
Property(
x => x.Dollars,
m => m.MapMoney()
);
public static void MapMoney(this IPropertyMapper mapper)
{
m.Precision(9);
m.Scale(6);
}
Same here: it makes the mapping definition more self descriptive.
(Yes I know that you don't want to change all the files, but I still recommend to put this information into the mapping files, because it is more explicit what the decimal actually is. It is very easy to change the mapping for all Money properties, but keep the Amount properties. For the completely implicit solution, read on.)
Alternatively you could use mapping conventions. These is very powerful. You still can overwrite the precision in the mapping file, which provides great flexibility.
mapper.BeforeMapProperty += MapperOnBeforeMapProperty;
private void MapperOnBeforeMapProperty(
IModelInspector modelInspector,
PropertyPath member,
IPropertyMapper propertyCustomizer)
{
Type propertyType;
// requires some more condition to check if it is a property mapping or field mapping
propertyType = (PropertyInfo)member.LocalMember.PropertyType;
if (propertyType == typeof(decimal))
{
propertyCustomizer.Precision(9);
propertyCustomizer.Scale(6);
}
}
It is also possible to put the user type into the mapping convention, as a default.