Monday 15 June 2009

Philosopical Difference between Values, Entities, and Events - a Matter of Existence

Dear Junior

There are numerous ways and rules-of-thumb whether to choose entities or whether to choose value objects when modelling a domain; but I have for long felt that there is a deeper philosophical distinction that I have still not been able to nail down – but now I stand the risk of a try. It is the distinction of their “nature of existence”.

For things that we view as entities, e g people, the set is clearly partitioned between those that do exist and those that don’t. For example, the person I reference as ‘me’ does exist (proof: cognito). On the other hand, the person ‘Dan’s brother-in-law’ does not exist, as none of my sisters is married (at the time of writing, and to my knowledge). Whether the person ‘the father’s father of Dan’ exists is of course a modelling choice – do we only model people alive, or anyone that has been alive? In either case the class ‘people’ is split in two disjoint and exempting subset, those that exist and those don’t.

This is also the reason for entities often come with a repository for finding specific entities (if they exist), and why we have to be prepared for handling entity-does-not-exist conditions. So the question of existence makes a huge difference in the use of entities.

For things that we view as values, e g colours, the question of existence become more academic. Does ‘blue’ exist? Well, there are definitely blue things around us, so we can say that it does exist. Does the colour defined by RGB (112.345, 43.23, 203.447) exist? Well there might be a thing in the world (even in our modelled part of the world) with exactly that colour. On the other hand there might be no thing with exactly that colour. The end point is that, whether there is such a thing or not does not feel relevant to the question of existence of the ‘colour as such’. So, if the mentioned colour exists, it does so rather in Plato’s world-of-ideas, rather in the physical one.

This is also the reason that value objects do not come with repositories. There is simply no usefulness that comes with answering the question ‘does blue exist’. The question of existence makes no difference in the use of values.

What finally set me on track for these thoughts was the use of ‘domain events’ (e g the arrival of a delivery, or a failed authentication) as a modelling tool. I think it is the ‘nature of existence’ that has felt awkward when modelling these as value objects, as that is what I have done. I simply modelled them as value objects because domain events are immutable, and so are value objects – but that is obviously (in hindsight) a line of reasoning en-par with “roses are red, so is father Christmas, ergo father Christmas is a rose". Now I see the faults of my way.

To my defence, I have also acknowledged that events either have-not-happened or have-happened, so there has been repository-like functionality from time to time to account for that distinction. So, they have been value objects with flavours of ‘entitiesm’ to them.

Finally, ‘domain events’ have sprung out of this Hegelian didactic tension. They are immutable as value objects, but they are not value objects as they have a conceptual identity. They have the same ‘nature of existence’ as entities, but they are not entities as they do not evolve their state over time.

Put another way: mutability makes the distinction between entities and domain events; substitutability makes the difference between entities and value objects; and ‘nature of existence’ makes the difference between value objects and domain events.

At least one way of putting it …