👨‍💻
Software Engineering
Implementing Domain Driven Design
Implementing Domain Driven Design
  • Intro
  • 1. Getting Started with DDD
    • The Business Value of DDD
  • 4: Architecture
  • 5: Entities
    • Why We Use Entities
    • Unique Identity
  • 6: Value Objects
    • Value Object Characteristics
    • Integrate with Minimalism
    • Standard Types Expressed as Values
    • Resources
  • 10: Aggregates
  • 8: Domain Events
    • The When & Why of Domain Events
    • Modeling Events
    • Publishing Events from the Domain Model
  • 13: Factories
  • Resources
Powered by GitBook
On this page
  • Ubiquitous Language
  • When is a concept a Value?
  • Measures, Quantifies, or Describes
  • Immutable
  • Conceptual Whole
  • Replaceability
  • Value Equality
  • Side-Effect-Free Behavior
  1. 6: Value Objects

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?

  1. It measures, quantifies, or describes a thing in the domain.

  2. It can be maintained as immutable

  3. It models a conceptual whole by composing related attributes as integral unit.

  4. It is completely replaceable when the measurement or description changes.

  5. It can be compared with others using Value equality.

  6. 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.

Previous6: Value ObjectsNextIntegrate with Minimalism

Last updated 7 months ago