【问题标题】:Set properties of an aggregate root child设置聚合根子节点的属性
【发布时间】:2020-02-24 07:22:42
【问题描述】:

我读到一篇关于 DDD 的文章,说 Aggregate 应该负责创建或更新 Aggregate Root Child。我唯一关心的是,如果子聚合有很多属性怎么办。

我通常做的就是这样

public class Parent
{

    public Child Child1{ get; protected set; }
    public void SetOrUpdateChild(string prop1, string prop2, string prop3, string prop4, string prop5, string prop6, string prop7, string prop8, string prop9, string prop10)
    {
        Child1 = Child.Create(prop1, prop2, prop3, prop4, prop5, prop6, prop7, prop8, prop9, prop10);
    }
}
public class Child
{
    public Child(string prop1, string prop2, string prop3, string prop4, string prop5, string prop6, string prop7, string prop8, string prop9, string prop10)
    {
        Prop1 = prop1;
        Prop2 = prop2;
        Prop3 = prop3;
        Prop4 = prop4;
        Prop5 = prop5;
        Prop6 = prop6;
        Prop7 = prop7;
        Prop8 = prop8;
        Prop9 = prop9;
        Prop10 = prop10;
    }

    public string Prop1 { get; protected set; }
    public string Prop2 { get; protected set; }
    public string Prop3 { get; protected set; }
    public string Prop4 { get; protected set; }
    public string Prop5 { get; protected set; }
    public string Prop6 { get; protected set; }
    public string Prop7 { get; protected set; }
    public string Prop8 { get; protected set; }
    public string Prop9 { get; protected set; }
    public string Prop10 { get; protected set; }

    public static Child Create(string prop1, string prop2, string prop3, string prop4, string prop5, string prop6, string prop7, string prop8, string prop9, string prop10)
    {
        return new Child(prop1, prop2, prop3, prop4, prop5, prop6, prop7, prop8, prop9, prop10);
    }
}

有没有更好的方法来做到这一点?我知道我可以将 Child 对象传递给 AddChild 方法,但我认为这不是理想的方法。我担心的是,如果 Child 对象增长,将很难管理。

【问题讨论】:

  • 然后使用构造对象,但这确实不适合stackoverflow

标签: c# .net domain-driven-design


【解决方案1】:

尝试使用 Builder 模式进行构建。

此外,您可以评估以将参数分组到有意义的值对象中。

【讨论】:

    【解决方案2】:

    抽象有问题,你没有找到它们。凝聚力可能被破坏了。研究 prop1 与 prop2 的关系,以及这些关系是否与 props 相同。如果不是,则应将它们组合在一起形成一个新的 ValueObject(街道、城市、Apt --> 地址)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-01-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-09-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多