ConceptChapter I · Domain · Building block 4 of 7

Entities & Value Objects

Entities are distinguished by their identity and their life cycle. Value Objects are fully described by their business values.

  1. EntityThe same identifier means the same object.
  2. Value ObjectThe same values mean the same.
  3. Comparison ofEntity over identifier, value over all values.

Context

Why this is important

Those who confuse identity and value receive false equality behavior: orders are compared by fields, amounts such as individual objects are tracked. This includes primitive obsession: an amount as a nude number knows no currency, and money never belongs in a floating point number. The consistency boundary of several objects is handled by the building block aggregates.

This building block explains identity and value equality. For the common consistency boundary of multiple objects see aggregates.

At a glance · Identity versus values

An entity keeps its identity through its identifier; a value object is defined by its values.
Shop example · Equality test

Is that the same thing?

Order B-1042 Despite the change of status, the same order remains. Two sums of money from 49,90 EUR are technically the same as soon as the amount and currency match.

This is how you read the picture

  1. Entity Order B-1042 Before and after: identifier the same, status different, yet the same.
  2. Value Object Amount of money 49,90 EUR: Amount and currency highlighted, both sides equal.
  3. Comparison of The highlighted fields show for each pair what is compared.

Order

  • NumberB-1042 (comparison field)
  • StatusDesigned for
  • Positions2 Cups

Order

  • NumberB-1042 (comparison field)
  • Statusacknowledged
  • Positions2 Cups

same orderComparison of Identification code. The identifier is the same. The status may change in the life cycle, the order remains the same.

Choose a pair; highlighted are the fields that are compared.

Entity and Value Object in Comparison

Entity and Value Object in Comparison
CharacterEntityValue Object
Shop exampleOrder B-1042Amount of money 49,90 EUR
EqualitySame order identification codeEqual amount and currency
ChangesStatus changes over life cycleNew value replaces old value
SignificanceTrack the same orderExpressing business value
The table summarizes what the pairs above show in detail.

Benefits & limitations

Helps if:

  • Primitive fields allow invalid or conflicting combinations.
  • Multiple values are validated, compared or calculated together.

Boundaries

  • Not every number needs a value object. Too many small values inflate the code. It is worthwhile where rules, validation or calculations depend on value, for example in the case of money with currency.
  • Persistence requires work: values need a mapping, such as embedded columns, and many ORMs expect object identifiers. Plan this mapping in Persistence from the outset.

In practice

  1. Identify the domain meaning and components. Group values that together form a rule or unit. Replace ambiguous primitive fields with a named concept.
  2. Define validation and equality. Specify valid combinations and when two values are equal in the domain. Use DDD Modelling to agree that meaning with domain experts.
  3. Safeguard operations on the value. Offer only calculations and comparisons that make sense for valid values. Check how the Value Object contributes to invariants within an Aggregate.
  4. Replace Primitive Obsession. Replace amount: number with Money(4990 cents, EUR) and validate it at creation. Store money in the smallest unit rather than as a floating-point number to avoid floating-point rounding errors.
Read the detailed explanation

A Value Object groups properties that together express a domain concept. Equality is determined by those components; changes are commonly represented by a new value. Validation at creation prevents invalid states. Entities are distinguished by stable identity; Aggregates use values to express invariants within a boundary. Domain-Driven Design places both concepts within a domain model.

Value Objects suit immutable concepts with well-defined operations. Equality must follow clear domain rules; reference equality alone is insufficient. Values should not share mutable internal state, so equality and validation remain valid throughout their lifetime.

An immutable interface is insufficient when internal references remain mutable. Values must not share mutable state, or a value could change without being replaced.

Check your understanding

Test yourself in three stages

  1. ReadingCheck in the picture

    Why apply 49,90 EUR; and 49,90 USD as different values, even though the amount is the same?

    View the solution with justification

    The amount and currency are compared together. If one of the two fields deviates, it is a different value.

  2. ApplyingTransferred to a new situation

    A position over 49,90 EUR shall be: 44,90 EUR discounted. What happens to the value?

    View the solution with justification

    The old value is not changed, but by a new amount of money from 44,90 EUR replaced.

  3. EvaluateWeighing an alternative

    Is an address a value object or an entity?

    View the solution with justification

    It depends on the context. In the order, only the information counts, i.e. a value. A customer management with an address book can keep it as an entity. Which context applies, clarifies the bounded context.

Continue on the learning path

Next in Domain modelling: Aggregate.

7 Building blocks

Continue withAggregate