Bidi关联和NHibernate映射 [英] Bidi associations and NHibernate mapping

查看:79
本文介绍了Bidi关联和NHibernate映射的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有BidiParent和BidiChildList类,它们以双向父子关系链接父母和孩子.如果孩子的父母是通过例如在服务层,新老父母的孩子列表会自动更新以反映更改.同样,如果父母的子女名单是通过例如添加新孩子后,该孩子的父母会自动更改,就像旧父母的孩子列表一样.我想尝试建立一个智能"域模型.

I have classes BidiParent and BidiChildList which link parents and children in a bidirectional parent-child relationship. If a child's parent is updated by e.g. the service layer, the old and new parents' children lists are automatically updated to reflect the change. Likewise, if a parent's children list is updated by e.g. adding a new child, the child's parent is automatically changed as is the old parent's children list. I want to try to build up a "smart" domain model.

显然,第一个问题是:这甚至是个好主意吗?

First question, obviously, is: is this even a good idea?

第二个问题是:是否可以告诉NHibernate访问和修改字段 _Children或_Parent,但可以认为属性子代或父代与完全相同场? NHibernate应该加载并保存内部字段,但是HQL或LINQ查询应该使用公共属性吗?

Second question is: is it possible to tell NHibernate to access and modify the field _Children or _Parent, but to consider the property Children or Parent to be entirely synonymous with the field? That NHibernate should load and save the internal fields, but HQL or LINQ queries should use the public properties?

public class BidiParent<C, P> { ... }

public class BidiChildList<C, P> : IList<C> { ... }

public class Parent {
    public string Name { get; set; }
    public IList<Child> Children {
        get { return ChildrenBidi; }
        set { ChildrenBidi.Set(value); }
    }
    private BidiChildList<Child, Parent> ChildrenBidi {
        get { return BidiChildList.Create(this, p => p._Children, c => c._Parent, (c, p) => c._Parent = p); }
    }
    internal IList<Child> _Children = new List<Child>();
}

public class Child {
    public string Name { get; set; }
    public Parent Parent {
        get { return ParentBidi.Get(); }
        set { ParentBidi.Set(value); }
    }
    private BidiParent<Child, Parent> ParentBidi {
        get { return BidiParent.Create(this, p => p._Children, () => _Parent, p => _Parent = p); }
    }
    internal Parent _Parent = null;
}

[Test]
public void MultilevelConstruction_Succeeds() {
    var p = new Parent {
        Name = "Bob",
        Children = new List<Child> {
            new Child { Name = "Kate" },
            new Child { Name = "Billy" }
        }
    };
    Assert.AreEqual(2, p.Children.Count);
    Assert.AreEqual("Kate", p.Children[0].Name);
    Assert.AreEqual("Billy", p.Children[1].Name);
    Assert.AreSame(p, p.Children[0].Parent);
    Assert.AreSame(p, p.Children[1].Parent);
}

推荐答案

回答我自己的问题:我需要使用命名策略,如

Answering my own question: I need to use a naming strategy, as detailed in the docs. RTFM right?

<class name="Parent">
    <property name="Name" />
    <bag name="Children" access="field.pascalcase-underscore">
        <key />
        <one-to-many class="Child" />
    </bag>
</class>
<class name="Child">
    <property name="Name" />
    <many-to-one name="Parent" access="field.pascalcase-underscore" />
</class>

这篇关于Bidi关联和NHibernate映射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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