动态组件流畅的自动映射 [英] dynamic-component fluent automapping

查看:123
本文介绍了动态组件流畅的自动映射的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我知道我们可以将普通的类作为组件映射,但是不能如何将字典映射为使用流畅自动映射的动态组件。



谢谢 解决方案

FluentNH 1.2.0.712 ):

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

公共类SomeClassMapping:ClassMap< SomeClass>
{
public SomeClassMapping()
{
Id(x => x.Id);

//将MyEnum成员映射到单独的int列。
DynamicComponent(x => x.Properties,
c =>
{
foreach(var Enum.GetNames(typeof(MyEnum)))
c .Map< int>(name);
});




$ b $ p
$ b

这里我们已经映射了一些Enum的所有成员所有这些列都是 int 类型。现在我正在处理一个场景,我们使用不同类型的动态列,而不是像这样:

  // ExtendedProperties包含名称和类型成员的自定义对象
foreach(ExtendedProperties中的var属性)
{
var prop = property;
part.Map(prop.Name).CustomType(prop.Type);
}

这也适用。



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



更新:
引用的情况是不幸的更复杂,请参阅此Google网上论坛帖子。简而言之:

  //这将不起作用
foreach(ExtendedProperties中的var属性)
{
var prop = property;
part.Reference(dict => dict [part.Name]);
}

//这个工程,但不是很动态
foreach(在ExtendedProperties var属性)
{
var prop = property;
part.Reference< PropertyType>(dict => dict [MyProperty]);



$ b $ p
$ p


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

解决方案

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.

这篇关于动态组件流畅的自动映射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆