有聚合根作为属性吗?

发布于 2025-01-07 10:40:34 字数 611 浏览 1 评论 0原文

聚合根可以有也是聚合根的子项吗?如果是,它是否仅引用它,或者保存另一个聚合根的属性的聚合根是否具有通过该方法更改它的方法?

假设您有一个名为“Worker”的类和另一个名为“Company”的类。两者都是聚合根。公司拥有工人的财产。 很抱歉做了一个坏榜样

public class Company {
    private Worker worker;

    ...

    public Worker getWorker() {
        ...
    }
}


public class Worker {
    ...
}

还是公司类“隐藏”了工人?

public class Company {
    private Worker worker;

    ...

    public String getWorkerName() {
        ...
    }
}


public class Worker {
    ...
}

工人是否只与公司类对话,因为它不是公司上下文的一部分?为什么会这样呢?包含在其他聚合根中的聚合根是否总是像我的第一个示例一样被访问? (我也这么认为,但没有理由)

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". 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)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

记忆里有你的影子 2025-01-14 10:40:34

简而言之,不。

这有点难以在 SO 答案中简洁地解释,但这里有一些要点可能有助于您的理解:

  • 聚合根是一致性边界。基本上,所有需要保持一致的数据都属于聚合根。当它具有高内聚性时,您就会知道它的结构正确 - 大多数方法都会涉及许多私有属性。
  • 与聚合根或其子项的所有交互都应通过聚合根上的公共方法完成。在您的示例中,如果工作人员属于公司,则应通过公司上的公共方法来添加/删除工作人员或对工作人员执行任何操作。
  • 可以在聚合根下面拥有实体,但根据我的经验,这通常是一种设计味道。根据我的经验,最常见的是,您将价值对象作为聚合的子对象。
  • 聚合根之间的关联是通过 ID 完成的,因此公司可能知道名为 WorkerId 的 GUID,但它绝对不应该耦合到 Worker 类。 (这将使您能够更轻松地重构。)
  • 如果操作(即方法)依赖于数据的先前状态,则只需要域模型(因此聚合根)。如果不涉及任何状态更改,则只需进行 CRUD 即可。

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:

  • 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. (This will allow you to refactor more easily.)
  • You only need a domain model (and therefore aggregate roots) if an operation (i.e. a method) depends on the previous state of the data. If there's no state changes involved, just do CRUD.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文