动态组件流畅的自动化

ili*_*ias 3 fluent-nhibernate

有谁知道如何在NHibernate中使用Fluent Automapping自动映射动态组件?

我知道我们可以将普通类映射为组件,但无法弄清楚如何使用流畅的自动化将字典映射为动态组件.

谢谢

Oli*_*ver 5

我们成功地使用了以下方法(使用FluentNH 1.2.0.712):

public class SomeClass
{
    public int Id { get; set; }
    public IDictionary Properties { get; set; }
}

public class SomeClassMapping : ClassMap<SomeClass>
{
    public SomeClassMapping()
    {
        Id(x => x.Id);

        // Maps the MyEnum members to separate int columns.
        DynamicComponent(x => x.Properties,
                         c =>
                            {
                                foreach (var name in Enum.GetNames(typeof(MyEnum)))
                                    c.Map<int>(name);
                            });
    }
}
Run Code Online (Sandbox Code Playgroud)

在这里,我们将一些Enum的所有成员映射到单独的列,其中所有成员都是int类型.现在我正在开发一个场景,我们使用不同类型的动态列,而不是:

// ExtendedProperties contains custom objects with Name and Type members
foreach (var property in ExtendedProperties)
{
    var prop = property;
    part.Map(prop.Name).CustomType(prop.Type);
}
Run Code Online (Sandbox Code Playgroud)

这也很有效.

我还要弄清楚的是如何使用References而不是Map引用具有自己的映射的其他类型...

更新: 不幸的是,参考的情况更复杂,请参阅此Google网上论坛主题.简而言之:

// This won't work
foreach (var property in ExtendedProperties)
{
    var prop = property;
    part.Reference(dict => dict[part.Name]);
}

// This works but is not very dynamic
foreach (var property in ExtendedProperties)
{
    var prop = property;
    part.Reference<PropertyType>(dict => dict["MyProperty"]);
}
Run Code Online (Sandbox Code Playgroud)

目前为止就这样了.