1
votes

In the book, Eric Evans shows an example where an VALUE OBJECT holds ENTITIES. VALUE OBJECTS are immutable, ENTITIES not.

The question is: If an ENTITY that is referenced from an VALUE OBJECT change its state, was the immutability been broken?

In my opinion it doesn't break the immutability, because the "value" of the object lies on the ENTITIES array, not on their states.

What do you guys think?

1
Well, if you want to work with immutable value objects (which is good), you can just get a new entity state object and throw away an old one. - Roman Eremin
I think it's not necessary, besides, the value object isn't aware of state changes in referenced entities, and that's right for me. - Marco Avila

1 Answers

0
votes

It depends if you can mutate the entities directly through the VO or not.

Imagine a VO instance that can be shared between multiple objects. If the VO contains a mutable entity and exposes it as a public member, multiple clients may update the entity concurrently. This leads to problems you would expect not to happen with actual immutability.

If the VO only holds the ID of the referenced entity on the other hand, you would have to fetch a brand new instance from a Repository before changing it, avoiding the shared mutable state issue.