域驱动设计 – 聚合根引用其他聚合根
我目前正在与DDD工作很多,并且在从其他聚合根加载/操作聚合根时面临一个问题。
Foreach在我的模型中聚合根,我也有一个存储库。现在,存储库负责处理根的持久性操作。 假设我有两个聚合根,一些成员(实体和Value对象)。 AggregateRoot1和AggregateRoot2 AggregateRoot1具有引用AggregateRoot2的实体成员 >当我加载AggregateRoot1,我应该加载aggregateRoot2吗? 此外,当我在AggregateRoot1中的实体与AggregateRoot2之间创建一个关联时,应该通过该实体完成,或者通过aggregateRoot2的存储库。 希望我的问题有道理。 [编辑] 当前解决方案 在Twith2Sugars的帮助下,我提出了以下解决方案: 如问题所述,“聚合根”可以包含引用其他根的子对象。当将root2分配给root1的其中一个成员时,root1的存储库将负责检测此更改,并将其委托给root2的存储库。 public void SomeMethod() { AggregateRoot1 root1 = AggregateRoot1Repository.GetById("someIdentification"); root1.EntityMember1.AggregateRoot2 = new AggregateRoot2(); AggregateRoot1Repository.Update(root1); } public class AggregateRoot1Repository { public static void Update(AggregateRoot1 root1) { //Implement some mechanism to detect changes to referenced roots AggregateRoot2Repository.HandleReference(root1.EntityMember1,root1.EntityMember1.AggregateRoot2) } } 这只是一个简单的例子,没有德米特法或其他最佳原则/做法包括:-) 进一步评论赞赏
也许AggregateRoot1存储库在构建AggregateRoot1实体时可以调用AggregateRoot2存储库。
我不认为这会使ddd无效,因为存储库仍然负责获取/创建自己的实体。 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |