gpt4 book ai didi

entity - DDD : Share entity with multiple aggregate roots

转载 作者:行者123 更新时间:2023-12-02 08:58:05 24 4
gpt4 key购买 nike

学习DDD,在我们的应用程序中存在三个聚合根,不同类型的表单,所有这些都需要上传一些PDF。这些上传的 pdf 文件附加了一些元数据,例如上传者和上传时间等,以便将它们存储在自己的表中。

我的问题是这个 PDF 是否应该建模为值对象、实体或聚合根。

对我来说,它看起来像一个名为“Attachment”的实体,但是这个实体应该由所有聚合根共享,只有类型而不是实例。是否允许在多个根中使用相同的实体类型,如果是的话,那么模型的哪一部分应该负责创建该实体。

更新

class Attachment{
Java.io.File pdf;
Date attachedOn;
.....
//no operation for this class
}

@AggregateRoot
class DocumentA {
Set<Attachment> attachments;
Set<DocumentB> supportingDocumentsB;
Set<DocumentA> supportingDocumentsA;
.... //other properties unique to DocumentA

attach(Attachment a);
detach(Attachment a);
addSupportingDocumentA(DocumentA doc);
removeSupportingDocumentA(DocumentA doc);
addSupportingDocumentB(DocumentB doc);
removeSupportingDocumentB(DocumentB doc);
.... //other operations unique to DocumentA
}

@AggregateRoot
class DocumentB {
Set<Attachment> attachments;
Set<DocumentB> supportingDocumentsB;
Set<DocumentA> supportingDocumentsA;
.... //other properties unique to DocumentB

attach(Attachment a);
detach(Attachment a);
addSupportingDocumentA(DocumentA doc);
removeSupportingDocumentA(DocumentA doc);
addSupportingDocumentB(DocumentB doc);
removeSupportingDocumentB(DocumentB doc);
.... //other operations unique to DocumentB
}

@AggregateRoot
class SomeAggregateRoot{
Set<Attachment> attachments;
//some properties

attach(Attachment a);
detach(Attachment a);
//some operations
}

现在的问题是如何将 Attachment 类建模为值对象(或)实体(或)聚合根。引用«Domain-Driven Design Distilled», Vaughn Vernon (ISBN-13: 978-0134434421) :

Furthermore, a Value Object is not a thing but is often used to describe, quantify, or measure an entity.

最佳答案

我认为附件概念更适合值对象概念,因为没有生命周期并且字段自然是不可变的。数据库条目具有主键这一事实并不意味着它必须是域上下文中的实体。

关于entity - DDD : Share entity with multiple aggregate roots,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47396674/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com