Value objects in DDD – Why immutable?

Ignore all the crazy answers about thread safe etc, that has nothing to do with DDD.
(I’ve yet to see a thread safe O/R mapper or other DDD friendly dal)

Imagine a value object for weights.
lets say we have a KG value object.

sample (edited for clarity):

var kg75 = new Weight(75);
joe.Weight = kg75;
jimmy.Weight = kg75;

Now what would happen if we do:

jimmy.Weight.Value = 82;

That would change the weight of joe too, if we are still using the same object references that is.
Note that we assigned an object representing 75kg to both joe and jimmy.
When jimmy gains weight, it is not the kg75 object that has changed, it is jimmys weight that has changed, thus, we should create a new object representing 82 kg.

But what if we have a new session and load both joe and jimmy in a clean UoW?

 var joe = context.People.Where(p => p.Name = "joe").First();
 var jimmy = context.People.Where(p => p.Name = "jimmy").First();
 jimmy.Weight.Value = 82;

What would happen then? well, since EF4 in your case would load joe and jimmy and their weights without any identity , we would get two different weight objects and when we change jimmys weight , joe would still weigh the same as before.

So we would have two different behaviours for the same code.
If the object references are still the same, then both joe and jimmy would get a new weight.
If joe and jimmy are loaded in a clean uow, only one of them would be affected by the change.

And that would be quite incosistent imo.

By using immutable VO’s, you would get the same behavior in both cases and you can still reuse object references for a smaller memory footprint when constructing object graphs.

Leave a Comment