Value Object Characteristics
Ubiquitous Language
Make certain when modeling a domain concept as a Value Object that you are addressing the Ubiquitous Language.
When is a concept a Value?
It measures, quantifies, or describes a thing in the domain.
It can be maintained as immutable
It models a conceptual whole by composing related attributes as integral unit.
It is completely replaceable when the measurement or description changes.
It can be compared with others using Value equality.
It supplies its collaborators with Side-Effect-Free-Behavior.
Measures, Quantifies, or Describes
A Value Object is a concept that measures, quantifies, or otherwise describes a thing in the domain.
Examples: Money, Name, Address, Phone, Email, etc.
Immutable
A object that is a Value is unchangeable after it has been created. None of the objects methods (public or private) can cause the state to change.
Challenge Your Assumptions: If the state of an object needs to be mutated, ask yourself why. Can replacement be used instead? If not, that's a strong indicator the object should be modeled as an Entity.
Conceptual Whole
Each attribute contributes an import part of a whole that collectively the attributes describe.
When taken apart, each attribute fails to provide a cohesive meaning.
Grouping of attributes accomplishes little if the whole fails to adequately describe another thing in the model.
A Value class' constructors should ensure that all attributes are initialized upon construction.
Don't allow the attributes of a Value instance to be populated after construction (i.e. building up the object piece by piece). The final state must be initialized all at once, atomically.
Replaceability
An immutable Value should be held as a reference within an Entity as long as the state describes the correct whole value.
If that is no longer true, the entire value is replaced with a new Value that represents the correct whole value.
Value Equality
Value is determined by comparing the types of both objects and then their attributes.
If both types and attributes are equal, the Values are considered equal.
Side-Effect-Free Behavior
Methods of a Value Object must all be Side-Effect-Free Functions because they must not violate the immutable quality.
Value Objects are not just value containers. They should have behavior and that behavior should result in the creation of new objects rather than modifications to the state of existing objects.
Last updated