简体   繁体   English

具有聚集的根作为属性?

[英]Having aggregate root as property?

Can aggregate roots have childs which also is aggregate roots? 聚合根是否可以具有子节点,而子节点又是聚合根? If yes, does it only reference it or does the aggregate root that holds a property of the other aggregate root have methods that changes it through that method? 如果是,它是仅引用它还是拥有其他聚合根属性的聚合根具有通过该方法更改它的方法?

Let say you have a class named "Worker" and another named "Company". 假设您有一个名为“ Worker”的类,另一个为“ Company”。 Both are aggregate roots. 两者都是聚合根。 Company have a property of Worker. 公司拥有工人财产。 Sorry for making a bad example 很抱歉做出不好的榜样

public class Company {
    private Worker worker;

    ...

    public Worker getWorker() {
        ...
    }
}


public class Worker {
    ...
}

or does the company class "hide" the worker? 还是公司类别“隐藏”工人?

public class Company {
    private Worker worker;

    ...

    public String getWorkerName() {
        ...
    }
}


public class Worker {
    ...
}

And does worker talk only to the company class because it is not part of the company context? 工人是否只与公司阶层对话,因为这不是公司背景的一部分? Why so? 为什么这样? Are aggregate roots contained inside other aggregate roots always accessed like my first example? 是否像我的第一个示例一样总是访问包含在其他聚合根中的聚合根? (I would think so, but I have no reason why) (我会这样认为,但是我没有理由)

In short, no. 简而言之,没有。

This is kinda tough to explain succinctly in a SO answer, but here's a few bullet points that may help your understanding: 在SO答案中很难简洁地解释这一点,但是这里有一些要点可以帮助您理解:

  • An aggregate root is a consistency boundary. 聚合根是一致性边界。 Basically, all the data that needs to be kept consistent belongs in the aggregate root. 基本上,所有需要保持一致的数据都属于聚合根。 You'll know it's structured correctly when it has high cohesion - most methods touch a lot of private properties. 当它具有很高的内聚力时,您会知道它的结构正确-大多数方法都涉及很多私有属性。
  • All interaction with an aggregate root or its children should be done via public methods on the aggregate root. 与聚合根或其子级的所有交互都应通过聚合根上的公共方法来完成。 In your example, if a Worker belongs to a Company then adding/removing a Worker or performing any operations on a Worker should be done via public methods on Company. 在您的示例中,如果某个工人属于公司,则应通过公司上的公共方法来添加/删除该工人或对该工人执行任何操作。
  • You can have entities below aggregate roots, but in my experience this is often a design smell. 可以将实体置于聚合根之下,但是以我的经验,这通常是设计的味道。 Most often in my experience, you have value objects as children of aggregates. 根据我的经验,大多数情况下,您将价值对象视为聚合的子对象。
  • Correlation between aggregate roots is done via an ID, so Company may know about a GUID called WorkerId, but it definitely should not be coupled to the Worker class. 聚合根之间的关联是通过ID完成的,因此公司可能知道一个名为WorkerId的GUID,但绝对不应将它与Worker类耦合。 (This will allow you to refactor more easily.) (这将使您更容易地重构。)
  • You only need a domain model (and therefore aggregate roots) if an operation (ie a method) depends on the previous state of the data. 如果操作(即方法)依赖于数据的先前状态,则仅需要域模型(因此需要聚集根)。 If there's no state changes involved, just do CRUD. 如果不涉及任何状态更改,请执行CRUD。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM