创建聚合实体的推荐方法
假设我有聚合名称“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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
根据聚合的定义,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.