dynamic-component fluent automapping

不问归期 提交于 2019-12-05 02:10:41

问题


Does anyone know how can we automatically map dynamic components using Fluent Automapping in NHibernate?

I know that we can map normal classes as components, but couldn't figure out how to map dictionaries as dynamic-components using fluent automapping.

Thanks


回答1:


We've used the following approach successfully (with 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);
                            });
    }
}

Here we've mapped all members of some Enum to separate columns where all of them are of type int. Right now I'm working on a scenario where we use different types for the dynamic columns which looks like this instead:

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

This also works very well.

What I'm still about to figure out is how to use References instead of Map for referencing other types that have their own mapping...

UPDATE: The case with References is unfortunately more complicated, please refer to this Google Groups thread. In short:

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

That's all for now.



来源:https://stackoverflow.com/questions/2834085/dynamic-component-fluent-automapping

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!