6

If I have a payment value object can it be shared by two different aggregate roots? or should I duplicate it?

Both options feel wrong!

Cool Breeze
  • 1,289
  • 11
  • 34
  • 1
    Is the answer in this post suggesting it is ok to share http://stackoverflow.com/questions/7477679/ddd-aggregates-and-value-objects? – Cool Breeze Nov 27 '14 at 02:05
  • Why do they feel wrong? A value object represents a value of a business concepts as defined by the language of the bounded context. – MikeSW Nov 27 '14 at 02:19
  • Actually it's only the duplication that feels wrong. Sharing it feels right... but what is Eric referring to when he says: "Nothing outside the Aggregate boundary can hold a reference to anything inside except to the root Entity"? – Cool Breeze Nov 27 '14 at 02:27
  • It means that anyone using the aggregate must do it via the aggregate root. In short, only the aggregate root is 'usable' directly. – MikeSW Nov 27 '14 at 03:36
  • That's what I thought. So a Value object in AR1 should not be accessible by AR2. The problem is the value object in AR1 makes perfect sense in AR2 as well... now I am starting to think the two aggregate roots belong in different BCs. – Cool Breeze Nov 27 '14 at 04:24
  • 2
    No, a value object is just a value, the 'reference' is for entities. It doesn't matter that the implementation of a value object is a class, the DDD _reference_ has nothing to do with an object reference from OOP. DDD doesn't care what programming language you're using. – MikeSW Nov 27 '14 at 04:33
  • right... so you're saying the same value object can be used inside different aggregates? – Cool Breeze Nov 27 '14 at 04:45
  • 1
    @user2909756 Yes, VO types can can be shared by any number of Aggregates *in the same BC*. – guillaume31 Nov 27 '14 at 08:24
  • 1
    The whole point of VOs being immutable is that they can be shared between various ARs/entities/clients without being afraid of side effects. – Alexander Langer Nov 27 '14 at 12:20

1 Answers1

7

As long as your VO is immutable, you can share it without any issues. However, Evans prohibits sharing mutable VOs and I think you will also agree with that.

plalx
  • 42,889
  • 6
  • 74
  • 90