Note that there are some explanatory texts on larger screens.

plurals
  1. POBidi associations and NHibernate mapping
    primarykey
    data
    text
    <p>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.</p> <p>First question, obviously, is: is this even a good idea?</p> <p>Second question is: is it possible to tell NHibernate to access and modify the <em>field</em> _Children or _Parent, but to consider the <em>property</em> 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?</p> <pre><code>public class BidiParent&lt;C, P&gt; { ... } public class BidiChildList&lt;C, P&gt; : IList&lt;C&gt; { ... } public class Parent { public string Name { get; set; } public IList&lt;Child&gt; Children { get { return ChildrenBidi; } set { ChildrenBidi.Set(value); } } private BidiChildList&lt;Child, Parent&gt; ChildrenBidi { get { return BidiChildList.Create(this, p =&gt; p._Children, c =&gt; c._Parent, (c, p) =&gt; c._Parent = p); } } internal IList&lt;Child&gt; _Children = new List&lt;Child&gt;(); } public class Child { public string Name { get; set; } public Parent Parent { get { return ParentBidi.Get(); } set { ParentBidi.Set(value); } } private BidiParent&lt;Child, Parent&gt; ParentBidi { get { return BidiParent.Create(this, p =&gt; p._Children, () =&gt; _Parent, p =&gt; _Parent = p); } } internal Parent _Parent = null; } [Test] public void MultilevelConstruction_Succeeds() { var p = new Parent { Name = "Bob", Children = new List&lt;Child&gt; { 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); } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload