I'm trying to migrate an existing HMB mapping file to Fluent mapping but have come unstuck mapping the following classed (simplified).
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"]); } }
}
We code against the interface IThing
and access its settings via the helper properties defined on the ISettings
interface. The settings are stored in the database in one table called Setting that's a bunch of key-value pairs with a foreign key to Thing.
The existing mapping file is as follows:
<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>
What I'm struggling with is the component definition because I can't find the Fluent equivalent of the class
attribute. Here's what I have so far:
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");
}
}