动态组件流畅自动映射

发布于 2024-09-01 04:34:50 字数 125 浏览 3 评论 0原文

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

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

谢谢

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

给妤﹃绝世温柔 2024-09-08 04:34:50

我们成功地使用了以下方法(使用 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);
                            });
    }
}

这里我们将某些 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);
}

这也非常有效。

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

更新:
遗憾的是,参考文献的情况更为复杂,请参阅此 Google 网上论坛帖子< /a>.简而言之:

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

现在就这些了。

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.

以酷 2024-09-08 04:34:50

我遇到了完全相同的问题。使用流畅的 nHibernate,我们无法映射这个问题,但我自己以某种方式能够解决这个问题。我的解决方案是动态构建 lambda 表达式并将其分配给对象。例如,

让我复制奥利弗引用的网站的一部分:

DynamicComponent(
    x => x.Properties,
    part =>
    {
        // Works
        part.Map("Size").CustomType(typeof(string));
        // Works
        var keySize = "Size";
        part.Map(keySize).CustomType(typeof(string));
        // Does not work
        part.Map(d => d[keySize]).CustomType(typeof(string));
 
        // Works
        part.References<Picture>(d => d["Picture"]);
        // Does not work
        var key = "Picture";
        part.References<Picture>(d => d[key]);
    });

我们遇到了这个问题,我们需要在映射中对“图片”进行硬编码。但不知何故,经过一些研究,我创建了以下解决方案:

var someExternalColumnNames = GetFromSomewhereDynamicColumns();

'x' is a DynamicComponent callback in fluent Nhibernate e.g. (DynamicColumns): DynamicComponent(a => a.DynamicColumns, x => (...content of method below...))

foreach(var x in someExternalColumnNames)
{
    if (x.IsReferenceToPerson == true)
    {
        var param = Expression.Parameter(typeof(IDictionary), "paramFirst");
        var key = Expression.Constant(x.Name);
        
        var me = MemberExpression.Call(param, typeof(IDictionary).GetMethod("get_Item"), new[] { key });
        var r = Expression.Lambda<Func<IDictionary, object>>(me, param);
            
        m.References<Person>(r, x.Name);    
    }
    else
    {
        m.Map(x.Name)
    }
}
//

// Some class that we want to reference, just an example of Fluent Nhibernate mapping
    
public class PersonMap : ClassMap<Person>
    {
        public PersonMap()
        {
            Table("Person");
            Id(x => x.PersonId, "PersonId");
            Map(x => x.Name);
        }
    }

    public class Person
    {
        public virtual Guid PersonId { get; set; }

        public virtual string Name { get; set; }

        public Person()
        { }
    }

也许会有帮助

I got struggle with exactly the same problem. With fluent nHibernate we cannot map this but on my own I somehow was able to solve this. My solution is to build lambda expression on the fly and the assign this into object. For instance, lets say that:

Let my copy part of the site that Oliver refer:

DynamicComponent(
    x => x.Properties,
    part =>
    {
        // Works
        part.Map("Size").CustomType(typeof(string));
        // Works
        var keySize = "Size";
        part.Map(keySize).CustomType(typeof(string));
        // Does not work
        part.Map(d => d[keySize]).CustomType(typeof(string));
 
        // Works
        part.References<Picture>(d => d["Picture"]);
        // Does not work
        var key = "Picture";
        part.References<Picture>(d => d[key]);
    });

And we have this problem that we need to hardcode "Picture" in mapping. But somehow after some research I created following solution:

var someExternalColumnNames = GetFromSomewhereDynamicColumns();

'x' is a DynamicComponent callback in fluent Nhibernate e.g. (DynamicColumns): DynamicComponent(a => a.DynamicColumns, x => (...content of method below...))

foreach(var x in someExternalColumnNames)
{
    if (x.IsReferenceToPerson == true)
    {
        var param = Expression.Parameter(typeof(IDictionary), "paramFirst");
        var key = Expression.Constant(x.Name);
        
        var me = MemberExpression.Call(param, typeof(IDictionary).GetMethod("get_Item"), new[] { key });
        var r = Expression.Lambda<Func<IDictionary, object>>(me, param);
            
        m.References<Person>(r, x.Name);    
    }
    else
    {
        m.Map(x.Name)
    }
}
//

// Some class that we want to reference, just an example of Fluent Nhibernate mapping
    
public class PersonMap : ClassMap<Person>
    {
        public PersonMap()
        {
            Table("Person");
            Id(x => x.PersonId, "PersonId");
            Map(x => x.Name);
        }
    }

    public class Person
    {
        public virtual Guid PersonId { get; set; }

        public virtual string Name { get; set; }

        public Person()
        { }
    }

Maybe it would be helpful

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文