NHibernate:在组件(复合元素)映射中映射受保护的成员 [英] NHibernate: Mapping protected members in component (composite-element) mapping

查看:80
本文介绍了NHibernate:在组件(复合元素)映射中映射受保护的成员的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个应用程序,该应用程序通过代码使用NHibernate映射,当我使用组件映射(相当于hbm复合元素映射)来存储值对象时,无法映射受保护的属性.

I'm building an application which uses NHibernate mapping by code, and I am unable to map protected properties when I use a component mapping (equivalent to hbm composite-element mapping) for a collection of value objects.

我能够在实体和组合映射中为单个值对象映射受保护的属性,只是映射值对象的集合时似乎不支持受保护的属性.

I am able to map protected properties in entity and compoment mappings for single value objects, it is just protected properties do not appear to be supported when mapping collections of value objects.

public class MyEntity
{
    public virtual int Id { get; protected set; }
    protected virtual MyValueObject MyValueObject { get; set; }
}

public class MyValueObject
{
    protected string SomeString { get; set; }
    protected ISet<NestedValueObject> NestedValueObjects { get; set; }
    // Constructor, Equals/GetHashcode overrides, etc.
}

public class NestedValueObject
{
    public string Name { get; set; }
    protected DateTime CreatedOn { get; set; } // Audit only property
    // Constructor, Equals/GetHashcode overrides, etc.
}

    public MyEntityMap()
    {            
        Table("MyEntityTable");
        Id(x => x.Id, map =>
        {
            map.Column("Id");
        });

        Component<MyValueObject>("MyValueObject", c =>
        {
            // Protected property maps correctly
            c.Property("SomeString", map =>
            {
                map.NotNullable(true);
            });
            c.Set<NestedValueObject>("NestedValueObjects", map =>
            {
                map.Table("NestedValueObjectsTable");
                map.Key(k => k.Column("MyEntityId"));
            }, r => r.Component(n =>
            {
                // Public property maps correctly
                n.Property(x => x.Name);
                // Compilation fail - there is no method that supports protected properties in component mappings
                n.Property<DateTime>("CreatedOn", map =>
                {
                    map.NotNullable(true);
                });
            }));
        });
    }

这是因为IMinimalPlainPropertyContainerMapper<TContainer>支持受保护的属性,而IComponentElementMapper<TComponent>不支持.

This is because IMinimalPlainPropertyContainerMapper<TContainer> supports protected properties, while IComponentElementMapper<TComponent> doesn't.

这是有原因的吗?似乎应该允许一个值对象具有受保护的属性,这些属性仅用于审计目的,并且不构成其概念标识的一部分,并且单个值对象的组件映射支持受保护的属性.

Is there a reason for this? It seems reasonable that a value object should be allowed to have protected properties which are for auditing purposes only and do not form a part of its conceptual identity, and protected properties are supported with the component mapping for single value objects.

推荐答案

这似乎是缺少的功能,而不是设计决定,它将在NHibernate的未来版本中修复:

It looks like this is missing feature, rather than a design decision, and will be fixed in a future release of NHibernate:

https://nhibernate.jira.com/browse/NH-3993

作为此版本之前的解决方法,替代方法是将属性公开或使用一对多映射将值对象映射为具有复合ID的实体,因为这些都支持受保护的变量.

As a workaround until this release, the alternatives would be to make the properties public or to map the value object as an entity with a composite id using a one-to-many mapping, since these support protected variables.

这篇关于NHibernate:在组件(复合元素)映射中映射受保护的成员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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