0

I'm facing the following issue: I have a very simple aggregate root Product, and a product contains Image entities.

Right now in code this relationship is expressed as a Many to One relationship like this:

class Product:
    Image[] images;

Now it happens that I need to make that relationship a Many to Many relationship, and a single image can be used by multiple products. Vaughn Vernon in one of its rules for aggregates states to only reference aggregate roots by ID to avoid transactional collisions.

Does that mean that I should turn the Entity Image into an aggregate root and change the product class to hold an array of Image IDs to solve my problem? What would be the best solution?

  • To solve what problem? – Robert Harvey Mar 15 '18 at 17:38
  • @RobertHarvey the problem of converting a many to one relationship to a many to many. – David Jiménez Martínez Mar 15 '18 at 18:16
  • Ah, I see. Well, I don't think that changes your aggregate root. You'll still refer to a Product by its ID. – Robert Harvey Mar 15 '18 at 18:56
  • 2
    Why is that a problem? Is there a use-case in your domain that requires a list of Products to be managed via an Image? I'm having trouble synthesizing such a requirement. Besides the fact that, to me, Image screams Value Object (making this problem moot), changing a domain model should be done with purpose - to accomplish a goal. Simply modifying your database schema doesn't necessarily mean your domain model should be changed. That's the whole idea of the abstraction right? – king-side-slide Mar 15 '18 at 20:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.