创建聚合实体的推荐方法

发布于 2024-10-14 08:29:16 字数 113 浏览 6 评论 0原文

假设我有聚合名称“ABCD”,它是根实体,并且该聚合中有另一个实体“IJKL”。我的问题是,创建 IJKL 实体并保留在数据库中的推荐方法是什么

Suppose, I have aggregate name "ABCD" which is root entity and another entity "IJKL" in that aggregate. My question is , what is the recommended way to create entity of IJKL and persist in database

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

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

发布评论

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

评论(1

感受沵的脚步 2024-10-21 08:29:16

根据聚合的定义,IJKL 的生命周期与 ABCD 的生命周期耦合,因此您不应该显式创建 IJKL;相反,您应该在 ABCD 上调用逻辑,它将决定(内部)如何/何时处理 IJKL。

对于新实例,聚合创建逻辑应封装在工厂中,并且该工厂将负责“更新”IJKL 实例以及聚合的其余部分。

对于持久性问题(加载/保存现有实体),逻辑应该是存储库的 SaveABCD 方法的一部分,该方法负责整个聚合的持久性。

如果您始终发现自己需要直接操作 IJKL,那么您的聚合就有问题 - ABCD 不是正确的根,或者 IJKL 需要位于单独的聚合中。

By the very definition of an aggregate the lifetime of IJKL is coupled to the lifetime of ABCD, so you shouldn't be creating IJKLs explicitly; instead you should be calling logic on ABCD which will decide (internally) how/when to deal with IJKL(s).

For new instances, the aggregate creation logic should be encapsulated in a factory, and that factory will be responsible for 'newing up' IJKL instances together with the rest of the aggregate.

For persistence concerns (load/save of existing entities) the logic should be part of the repository's SaveABCD method, which is responsible for persistance of the entire aggregate.

If you consistently find yourself with the need to manipulate IJKL(s) directly then there is a problem with your aggregate - either ABCD is not the correct root, or IJKL needs to be in a separate aggregate.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文