通过不同列的代码重用来进行nhibernate组件映射

时间:2012-04-17 15:01:10

标签: c# nhibernate nhibernate-mapping

我想在多个实体上重复使用Period Class。它基于一个遗留模式,其中列名不一致。 由于我想在几个场合重复使用这个句号类,我想我提供了多个映射与其对应的列名。但是我的计划没有用,因为nhibernate似乎只对每个实体使用一个映射,即使它是一个组件。

是否有其他方法可以将此Component与不同的基础列名重用?

public class Period
{
    public virtual DateTime From {get;set;}
    public virtual DateTime To {get;set;}
}


public class PeriodMapping
{
    public static Action<IComponentMapper<Period>> ForPolicy()
    {
        return c =>
        {
            c.Property(p => p.From, map => map.Column("CommencementDate"));
            c.Property(p => p.To, map => map.Column("ReasonForEnding"));
        };
    }

    public static Action<IComponentMapper<Period>> ForPasswordOfUser()
    {
        return c =>
        {
            c.Property(p => p.From, map => map.Column("PasswordValidFrom"));
            c.Property(p => p.To, map => map.Column("PasswordValidUntil"));
        };
    }

    // several other mappings
}

public class PolicyMapping : ClassMapping<Policy>
{
    public VertragMapping()
    {
        Component(a => a.Period, PeriodMapping.ForPolicy());
    }
}

public class UserMapping : ClassMapping<User>
{
    public VertragMapping()
    {
        Component(a => a.Period, PeriodMapping.ForPasswordOfUser());
    }
}

1 个答案:

答案 0 :(得分:0)

你可以尝试这样做。

public class PeriodMapping
{
    public static Action<ComponentPart<Period>> ForPolicy()
    {
        return c =>
        {
            c.Map(p => p.From, "CommencementDate");
            c.Map(p => p.To, "ReasonForEnding");
        };
    }

    public static Action<ComponentPart<Period>> ForPasswordOfUser()
    {
        return c =>
        {
            c.Map(p => p.From, "PasswordValidFrom");
            c.Map(p => p.To, "PasswordValidUntil");
        };
    }

    // several other mappings
}