将结构映射为类型(可以为null)

本文关键字:null 类型 结构 映射 | 更新日期: 2023-09-27 18:29:26

我有一个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);
}

这应该分别作为CCD_ 4和CCD_?

优选地,这将不必编辑每个映射,因此我宁愿不使用IUserType,因为这需要(我认为)将放在各处

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

将结构映射为类型(可以为null)

您想要的是通过组件以类似于以下代码摘录的方式实现的:

 Component(x => x.Price, m =>
{
m.Map(money => money.Amount, "Price_Amount");
m.Map(money => money.CurrencyCode, "Price_CurrencyCode");
});

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