DDD during lunchbreak

Value Objects

Introduction

In this article, we'll delve into the world of Domain Driven Design (DDD) during your lunch break and demystify the concept of Value Objects. We'll explore what a Value Object is and how it can be effectively utilized in your code. While we'll be using PHP for our examples, the core concept is applicable across various programming languages.

Understanding a value object

A value object is a pattern that is used to encapsulate a value. It plays a big part in the Domain Driven Design (DDD) approach. A value object is immutable, which means that it can't be changed after it was created. Additionally, it should be compared by its value and not by its identity.
Here is an example of a value object in PHP:

            class Name
            {
                public function __construct(private string $firstName, private string $lastName)
                {
                    if ($firstName === '' || $lastName === '') {
                        throw new InvalidArgumentException('The first and last name cannot be empty.');
                    }
                }

                public function firstName(): string
                {
                    return $this->firstName;
                }

                public function lastName(): string
                {
                    return $this->lastName;
                }

                public function formatted(): string
                {
                    return $this->firstName . ' ' . $this->lastName;
                }
            }
        
In this example, the Name class is intentionally straightforward. It consists of two properties, a constructor, and three methods. The constructor is the sole injection point, ensuring that the object cannot be created without the necessary data. It's a critical detail, especially for data validation. If any data validation is required, it should be performed within the constructor.

Immutability a super power

The important part is that the object is immutable. This means that the properties can't be changed after the object was created. So the constructor has to validate the data and throw an exception if it is invalid, so the rest of your code base can be sure that the data is valid.

Utility methods

The Name class also provides two simple getter methods for retrieving the first and last names. These methods serve as straightforward accessors, without any additional logic. Furthermore, the "formatted" method offers a convenient way to obtain the full name, a common requirement within the domain.

Characteristics of a value object

A value object should contain the following characteristics
  • Immutability

    The object must not change once created.
  • Lack of Identity

    It has no unique identity and is solely defined by its value.
  • Defines Allowed Values

    A Value Object defines the permissible range of values.
  • Easy to Test

    Value Objects are inherently testable due to their predictability.
  • No-Brainer Usage

    Incorporating Value Objects should be straightforward and intuitive in your codebase.

Conclusion

Value Objects are essential components in DDD, contributing to a more robust and predictable domain model. By embracing immutability and encapsulating values, you can enhance the integrity of your codebase and simplify data management. The simplicity of Value Objects makes them an invaluable tool for developers. So, consider implementing them in your projects and witness the benefits of this powerful DDD pattern.

All rights reserved © Niels Theen