Map struct as type (which can be null)
I have struct
one that wraps decimal
so I can use EditorTemplates, have a security type, expand in the future to add Currency
, etc. etc.:
public struct Price
{
private readonly decimal value;
public Price(decimal value)
{
this.value = value;
}
public static implicit operator Price(decimal value)
{
return new Price(value);
}
public static explicit operator decimal(Price price)
{
return price.value;
}
}
This is then used in many types that are rendered using Fluent NHibernate, for example:
public class SomeEntity
{
public virtual int Id { get; protected set; }
public virtual Price SomePrice { get; set; }
public virtual Price? NullablePrice { get; set; }
}
How can I tell FNH that when I do
public SomeEntityMap()
{
this.Id(x => x.Id);
this.Map(x => x.SomePrice);
this.Map(x => x.NullablePrice);
}
that it should just be treated as decimal
and Nullable<decimal>
accordingly?
Preferably it would be without the need to edit every mapping, so I'd rather not use IUserType
it as it requires (I think) placing it everywhere
this.Map(x => x.SomePrice).CustomType<PriceType>();
this.Map(x => x.NullablePrice).CustomType<NullablePriceType>();
source to share
What you want can be achieved via Components in the same way as the following code snippet :
Component(x => x.Price, m =>
{
m.Map(money => money.Amount, "Price_Amount");
m.Map(money => money.CurrencyCode, "Price_CurrencyCode");
});
Components are an ideal method for mapping normalized data to a reusable object.
source to share