2013-01-24 111 views
0

我想遷移一個現有的HMB映射文件到Fluent映射但已經來映射以下分類(簡化)。流利NHibernate映射字典包裝類

public interface IThing 
{ 
    int Id { get; } 
    string Name { get; } 
    ISettings Settings { get; } 
} 

public class Thing : IThing { /* Interface implementation omitted */ } 

public interface ISettings 
{ 
    string SomeNamedSetting1 { get; } 
    bool SomeNamedSetting2 { get; } 
    int SomeNamedSetting3 { get; } 
} 

public class Settings : ISettings 
{ 
    Dictionary<string, string> rawValues; 

    public string SomeNamedSetting1 { get { return rawValues["SomeNamedSetting1"]; } } 
    public bool SomeNamedSetting2 { get { return Convert.ToBoolean(rawValues["SomeNamedSetting2"]); } } 
    public int SomeNamedSetting3 { get { return Convert.ToInt32(rawValues["SomeNamedSetting3"]); } } 
} 

針對接口IThing我們的代碼,並通過ISettings接口上定義的輔助性訪問其設置。這些設置存儲在數據庫中的一個名爲「設置」的表中,這是一組使用Thing外鍵的鍵值對。

現有的映射文件如下:

<component name="Settings" lazy="false" class="Settings, Test"> 
    <map name="rawValues" lazy="false" access="field" table="Setting"> 
    <key column="Id" /> 
    <index column="SettingKey" type="String" /> 
    <element column="SettingValue" type="String" /> 
    </map> 
</component> 

我正在掙扎是組件定義,因爲我無法找到流利相當於的class屬性。這是我到目前爲止:

public class ThingMap : ClassMap<Thing> 
{ 
    public ThingMap() 
    { 
     Proxy<IThing>(); 
     Id(t => t.Id); 
     Map(t => t.Name); 

     // I think this is the equivalent of the private field access 
     var rawValues = Reveal.Member<Settings, IDictionary<string, string>>("rawValues"); 

     // This isn't valid as it can't convert ISettings to Settings 
     Component<Settings>(t => t.Settings); 

     // This isn't valid because rawValues uses Settings, not ISettings 
     Component(t => t.Settings, m => 
     { 
      m.HasMany(rawValues). 
      AsMap("SettingKey"). 
      KeyColumn("InstanceId"). 
      Element("SettingValue"). 
      Table("Setting"); 
     }); 

     // This is no good because it complains "Custom type does not implement UserCollectionType: Isotrak.Silver.IInstanceSettings" 
     HasMany<InstanceSettings>(i => i.InstanceSettings). 
      AsMap("SettingKey"). 
      KeyColumn("InstanceId"). 
      Element("SettingValue"). 
      Table("Setting"); 
    } 
} 

回答

1

對於同一條船上的任何人,我最終破解它。

Component<Settings>(i => i.Settings, m => 
{ 
    m.HasMany(rawValues). 
    AsMap<string>("SettingKey"). 
    KeyColumn("InstanceId"). 
    Element("SettingValue"). 
    Table("Setting"); 
}); 

現在看起來很明顯!