如何生成一个组合根,它创建一个具有向后(父引用)的对象树。
想象一下我想构建一个二叉树的以下简单节点对象:
class Node
{
public Node Parent{get;}
public Node Child1{get;}
public Node Child2{get;}
public Node(Node parent, Node child1, Node child2)
{
Parent = parent;
Child1 = child1;
Child2 = child2;
}
}
这是我卡住的地方:
public void CompositionRoot()
{
//example of a parent node with
Node Parent = new Node
(
null, //the parent has no parent node
new Node
( /* ??? here should come the reference to the parent node,
but since it's inside its own constructor I have
no reference yet */
, null //the child node contains no further child
, null
)
)
}
我是否因为它包含循环依赖关系而尝试将依赖注入原理与这种对象图相结合而走错了路?解决方案是什么?