It may be just me ... but it looks like a bad design.
If this is the true Parent → Child relationship, you should not allow anyone to create an orphan. Therefore, the parent must be installed on the child at the time of creation.
I would probably do something like:
class ChildClass
{
private ParentClass _parent;
public ChildClass(ParentClass parent)
{
_parent = parent;
}
}
And then:
class ParentClass
{
private List<ChildClass> _children;
public virtual ReadOnlyCollection<ChildClass> Children
{
get
{
return _children.AsReadOnly();
}
}
public virtual ChildClass CreateChild()
{
ChildClass newChild = new ChildClass(this);
_children.Add(newChild);
return newChild;
}
}
source
share