DDD:共享多个聚合根

时间:2017-11-20 16:28:12

标签: entity domain-driven-design aggregateroot value-objects

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

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

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

更新

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)

  

此外,价值对象不是一个东西,但通常用于描述,量化或衡量一个实体。

1 个答案:

答案 0 :(得分:1)

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

相关问题