2012-04-17 79 views
0

我想在多個實體上重用一個Period類。它基於列名稱不一致的傳統模式。 因爲我想多次重複使用這個期間課程,所以我認爲我提供了與他們的通訊員列名的多個映射。但是我的計劃沒有奏效,因爲nhibernate似乎只對每個實體都有一個映射,即使它是一個組件。通過代碼重用與不同列的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()); 
    } 
} 

回答

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 
} 
相關問題