Aug
12
2010

Value objects are only just playing a role

Recently I had an interesting discussion on the DDD mailing list about value objects.

Value objects: “These are the objects that describe things” [From: DDD, Evans]

The discussion centered on the immutability of value objects. The strange thing is that many developers (in the role a modeller) emphasize this immutability. I’ve seen this happen quite often. While I do not think that is the essence of a value object at all.

The essence of a value object is that it is an object whose value is important, not its identity. Each value object is thus interchangeable for any other object with the same value.

Because they are interchangeable, value objects are often shared. This leads to the technical requirement for each value object to be immutable, because otherwise, when you modify a value object, the other object that refers to the same value, suddenly refers to another value without it expecting it. So if I for instance change my name, everyone called “Rick van der Arend” suddenly would be called otherwise. Fortunately, there are not going to be many of them, because my name is not very common. But imagine being called John Doe.

So, that is clear enough: value objects represent values, are often immutable and all value objects with the same value are conceptually the same object.

But a model is only valid within it´s context
But: a model applies to a particular context. This also means that the object within one context might be a value object, but outside that context could very well be an entity. In the context in which the object is an entity, changes to the properties of the object will not make you see it as being another object. On page 98 in the blue book, Evans is specifically referring to this kind of situation, which he explains by using the example that an address generally is a value object for the person sending mail, but probably an entity to the postal service (that defines addresses).

I think you see this pattern often with objects that are being treated as value objects during a bootup phase in a configuration context and after bootup (and a context switch) will be entities in their own, runtime domain. Consider also the case of a classical data access layer, that shuttles all of your precious objects back-and-forth to the database without worrying about their identities: if all attributes (including ID) are the same, then it’s just the same object to it. (OR mappers, such as Hibernate, elaborate on that and actually translate it into an entity and keep track of it as such).

And what does this knowledge bring you?
Of course this is of interest to those engaged in modelling, but in most cases you design a model for a given context and you will not run into this problem that often. Right?

What I see happen quite often is that immutability is used as a criterion for determining whether something is a value object, and that this criterion is imposed on the (entire) object, in reality. With an established label such as a color, this will often work out ok, as those normally do not change. With a numerical value, such as an age, it becomes a little bit more difficult because someone´s age can change. Yes, but the age 30 stays the age 30. In other words: the number 30 will be the number 30. So still not a lot of problems there.

But sometimes a whole class of value objects is being missed: those objects that have a clear identity, but within the context in which they are being modelled, they are not. Remember: if an object in your context is completely defined by its attributes, it’s a value object.

Example
I will close this off with an example: the search for an apartment in Barcelona. For you, the one searching, it does not matter which apartment exactly is which and how the appartment was last year. It is the number of square meters, the city location, the number of rooms, the presence of a bath, etc. that matter to you. For the owner of several of these appartments it is a whole different story: he wants to maintain his apartment and a newly renovated apartment has not suddenly become a totally different apartment, even though it now  has got more rooms. Of course it is true that once you, as a guest, know about a specific apartment and really like it, that might be the same one where you´ll want to go to next year. If it stays the same, of course ..

 

Written by Rick | Tags: , , , , , |

No Comments - Leave a comment »

RSS feed for comments on this post. TrackBack URL

Leave a comment

Powered by WordPress | Aeros Theme | TheBuckmaker.com WordPress Themes

Better Tag Cloud