父母子女关系模式

父母子女关系模式

本文介绍了领域驱动设计 - 父母子女关系模式 - 规范模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道以下哪些被认为是在与父子关系处理是一个最佳实践。

I was wondering which of the following is considered to be a best practice when dealing with parent child relationships.

1)以下的例子似乎是一种常见的做法,但创建子的实例时,这将是处于无效状态,只要它不添加到父。莫非不是这导致有关验证等问题。

1) The following example seems to be a common practice, but when creating an instance of a child, it will be in an invalid state as long as it is not added to the parent. Couldn't this lead to problems regarding validation etc.

public class Parent
{
    private ICollection<Child> children;

    public ReadOnlyCollection Children { get; }

    public void AddChild(Child child)
    {
        child.Parent = this;
        children.Add(child);
    }
}


public class Child
{
    internal Parent Parent
    {
        get;
        set;
    }

    public Child()
    {
    }
}

2)下一个示例会照顾,孩子必须始终与其父。

2) The next sample would take care that a child must always be related to its parent.

public class Parent
{
    private ICollection<Child> children;

    public ReadOnlyCollection Children { get; }

    public Child CreateChild()
    {
        var child = new Child();
        child.Parent = this;
        children.Add(child);
        return child;
    }
}


public class Child
{
    internal Parent Parent
    {
        get;
        set;
    }

    internal Child()
    {
    }
}

3)在那个孩子取相对于它的母公司照顾最后一个例子。

3) In the last example that child takes care of the relation to its parent itself.

public class Parent
{
    private ICollection<Child> children;

    public ReadOnlyCollection Children { get; }

    public void AddChild(Child child)
    {
        child.Parent = this;
        children.Add(child);
    }
}


public class Child
{
    public Parent Parent
    {
        get;
        set;
    }

    public Child(Parent parent)
    {
        this.Parent = parent;
    }
}

哪个模式被认为是最好的?我相信这种模式2可能是最好的,因为那么一个孩子可以没有关系,其父永远存在。这将使它更容易例如落实可能做的事情一样的规范模式:

Which pattern is considered the best? I believe that pattern 2 might be the best since then a child can never exist without a relation to its parent. This would make it easier e.g. when implementing a specification pattern that might do things like:

public class ChildSpecification
{
    bool IsSatisfiedBy(Child child)
    {
        return child.Parent.Children.Where(someCondition).Count > 0;
    }
}

以上规格只能工作,如果一个孩子有父母。

The above specification can only work if a child has a parent.

你怎么看?你知道更好的方法?在此先感谢

What do you think? Do you know better ways? Thanks in advance

推荐答案

我绝对喜欢建议2号,但我认为它忽略一些重要的事情是在3个发现,即如果一个孩子对象不能没有应采取在其构造对象存在。此外,孩子类的属性应为只读。所以,你最终会是这样的:

I definitely like suggestion number 2, but I think that it misses something important that is found in 3, namely that if a Child object cannot exist without a Parent it should take a Parent object in its constructor. Furthermore the Parent property on the Child class should be read only.So you would end up with something like:

public class Parent
{
    private ICollection<Child> children;

    public ReadOnlyCollection Children { get; }

    public Child CreateChild()
    {
        var child = new Child(this);
        children.Add(child);
        return child;
    }
}


public class Child
{
    internal Parent Parent
    {
       get;
       private set;
    }

    internal Child(Parent parent)
    {
       this.Parent = parent;
    }
}

这篇关于领域驱动设计 - 父母子女关系模式 - 规范模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-12 19:14