0

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

有没有其他方法可以用不同的底层列名重用这个组件?

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());
    }
}
4

1 回答 1

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
}
于 2012-10-10T23:42:57.377 回答