DDD: Can immutable objects also be entities

domain-driven-design

I've read countless posts on differences between Entities and Value objects and while I do think that at least conceptually I understand how the two differ, it appears that in some of these posts authors consider a particular domain concept to be a VO simply because it is immutable ( thus its state will never change, at least within that particular domain model ).

Do you agree that if the state of an object will never change within particular domain model, then this object should never be an entity? Why?

Best Answer

Going by the book (Evans, 2004), "An object defined primarily by its identity is called an ENTITY". This definition is independent of whether the object is mutable or immutable. I think it's much less likely for an immutable object to be an entity in a given domain, so it's a useful heuristic for deciding whether an object is a "value object" or an "entity", but that's not part of the definition.

For example, let's say you have an entity representing an employee, who may or may not have a direct supervisor. If you decide to represent the idea of not having a direct supervisor as being a reference to a "null" supervisor object, then the "null" supervisor object is reasonably considered an entity. And you could probably make this "null" object immutable.