Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DDD: Share entity with multiple aggregate roots

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.

like image 918
wolverine Avatar asked Nov 08 '22 14:11

wolverine


1 Answers

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.

like image 56
Marco Avila Avatar answered Nov 21 '22 15:11

Marco Avila