What is the difference between an entity and a value object in domain modelling?
Quick Answer
An entity has a distinct identity that persists through changes — a Customer is the same customer even after they change their name. A value object has no identity; it's defined entirely by its attributes and two with the same values are interchangeable — Money, Address, DateRange. Value objects are immutable.
An entity is something you track by identity over time — a customer, an order. Even if every attribute changes, it's still the same entity because the id is the same, so equality is id-based. A value object has no identity; it just describes something, and two value objects with identical contents are completely interchangeable — 5 USD is 5 USD. So value objects are compared by their fields and they're immutable. The practical win is wrapping primitives: making Money a value object rather than passing decimal everywhere gives you one place for currency and rounding rules, and you can't accidentally add dollars to euros.
- Entity equality is by id; value-object equality is by all fields (records fit well).
- Modelling a concept like
Moneyas a value object instead of a baredecimalcentralises rounding, currency rules and validation. - Value objects have no lifecycle of their own — they're created, used, discarded, and owned by an entity/aggregate.
- An aggregate root is a specific entity that guards a cluster of entities and value objects as a consistency boundary.