我目前正在与DDD工作很多,并且在从其他聚合根加载/操作聚合根时面临一个问题。
Foreach在我的模型中聚合根,我也有一个存储库。现在,存储库负责处理根的持久性操作。
假设我有两个聚合根,一些成员(实体和Value对象)。
AggregateRoot1和AggregateRoot2
AggregateRoot1具有引用AggregateRoot2的实体成员
>当我加载AggregateRoot1,我应该加载aggregateRoot2吗?
>它是否是对于负责此处的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无效,因为存储库仍然负责获取/创建自己的实体。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。