C# 将结构映射为类型(可以为空)

C# 将结构映射为类型(可以为空),c#,nhibernate,fluent-nhibernate,C#,Nhibernate,Fluent Nhibernate,我有一个struct,它包装了decimal,因此我可以使用EditorTemplates,具有类型安全性,将来扩展以添加Currency,等等: public struct Price { private readonly decimal value; public Price(decimal value) { this.value = value; } public static implicit operator Price(de

我有一个
struct
,它包装了
decimal
,因此我可以使用EditorTemplates,具有类型安全性,将来扩展以添加
Currency
,等等:

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;
    }
}
然后在使用Fluent NHibernate映射的许多类型中使用此选项,例如:

public class SomeEntity
{
    public virtual int Id { get; protected set; }
    public virtual Price SomePrice { get; set; }
    public virtual Price? NullablePrice { get; set; }
}
当我这么做的时候,我怎么能告诉FNH

public SomeEntityMap()
{
    this.Id(x => x.Id);
    this.Map(x => x.SomePrice);
    this.Map(x => x.NullablePrice);
}
这应该分别被视为
十进制
可空

最好是不必编辑每个映射,因此我不希望使用
IUserType
,因为这需要(我认为)到处放置

this.Map(x => x.SomePrice).CustomType<PriceType>();
this.Map(x => x.NullablePrice).CustomType<NullablePriceType>();
this.Map(x=>x.SomePrice.CustomType();
this.Map(x=>x.NullablePrice).CustomType();

您想要的东西可以通过以下方式实现:


组件是将规范化数据映射到可重用实体的完美方法。

您能解释一下您的意思吗?因此,映射的将是
受保护的虚拟十进制小数{get;set;}
,公共财产将是
公共价格{get…
使用十进制值…是否更清楚一点?@RadimKöhler,谢谢,这更清楚,但是在较低的区域有一个巨大的疼痛,因为整个项目中有这么多这样的区域。你说“应该被视为十进制”是什么意思,你能举个例子吗?在你的映射中,x.SomePrice是十进制还是Price结构?如果你想要十进制的行为,那么在结构上实现数学运算符?@Cybrosys问题更新为示例:)谢谢你的回答。当它是
可空的时候这也可以吗?是的,应该。确保数据库字段it映射到null也是可以的。
 Component(x => x.Price, m =>
{
m.Map(money => money.Amount, "Price_Amount");
m.Map(money => money.CurrencyCode, "Price_CurrencyCode");
});