0

我正在尝试将现有的 HMB 映射文件迁移到 Fluent 映射,但在映射以下类(简化)时遇到了问题。

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。这些设置存储在数据库中的一个名为 Setting 的表中,该表是一组键值对,具有 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属性的 Fluent 等效项。这是我到目前为止所拥有的:

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

1 回答 1

1

对于同一条船上的任何人,我最终破解了它。

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

现在看起来很明显!

于 2013-01-28T13:34:10.873 回答