DDD: Share entity with multiple aggregate roots

孤人 提交于 2019-12-07 11:12:48

问题


Learning DDD, in our application there are three aggregate roots, different types of forms, all of which needs some PDF to be uploaded. These pdf uploads have some metadata, like who uploaded and when uploaded etc, attached to it so they are stored in their own table.

My question is whether this PDF should be modeled as a value object or an entity or an aggregate root.

For me it looks like an entity named as "Attachment" but then this entity should be shared, only the type not the instances, by all the aggregate root. Is it an allowed practice to use same entity type in multiple roots, if so then which part of the model should take the responsibility for creating this entity.

Update

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
}

Now the question is how the Attachment class should be modeled, as a value object (or) an entity (or) an aggregate root. Quoting «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.


回答1:


I think the Attachment concept fits better in the Value Object concept, since there is no life cycle and the fields are naturally immutable. The fact that the data base entry has an primary key doesn't imply it has to be an entity in the domain context.



来源:https://stackoverflow.com/questions/47396674/ddd-share-entity-with-multiple-aggregate-roots

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!