DDD during lunchbreak

Entity Objects

Introduction

In this article we'll look what an entity 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 an entity object

An entity object is pattern that encapsulates a value that has an identity. It plays a big part in the Domain Driven Design (DDD) approach. Different from a value object, an entity object is mutable, which means that it can be changed after it was created. The entity object should be compared by its identity and not by its value. This means that two entity objects with the same value may not equal.
Here is an example of an entity object in PHP:

class User
{
    public function __construct(
        private Email $email,
        private Name $name
    ) {
    }

    public function email(): Email
    {
        return $this->email;
    }

    public function name(): Name
    {
        return $this->name;
    }

    public function changeEmail(Email $email): void
    {
        $this->email = $email;
    }

    public function changeName(Name $name): void
    {
        $this->name = $name;
    }

    public function equals(User $user): bool
    {
        $isEmailEqual = $this->email()->equals($user->email());
        $isNameEqual = $this->name()->equals($user->name());

        return $isEmailEqual && $isNameEqual;
    }
}
        
As you can see, the User class consists of two value objects, Email and Name. This is a characteristic of an entity object, it contains of value objects. You can also that different to a Value object, an entity can change its attributes after it was created. It is mutable. Entity objects are by default harder to test than entity objects, because they represent a part of your business logic ans has a life cycle.

🦁 Life cycle - The circle of life

A life cycle means that an entity object can change over time. This means that the object can be created, updated and deleted. This is a big difference to a value object, which is immutable and can not be changed after it was created.

πŸ•΄πŸ» Identity - Who am I?

An entity object has an identity. This means that two entity objects with the same value may not equal. The identity is a unique identifier for the object. This is important because it allows you to compare two objects by their identity and not by their value. The comparability is based on your business logic. For example, two users with the same email address are not the same user or are they? This depends on your domain you are working in. In some domains two users with the same email address are the same user, in other domains they are not.

πŸ”Ž Characteristics of an entity object

A entity object should contain the following characteristics
  • πŸ•΄πŸ» Identity

    The object must represent a domain value.
  • 🦁 Life cycle

    The object must represent a domain value.
  • 🧬 Evolve over time

    This is object can evolve and change overtime
  • πŸ’Ž Needs value objects

    An entity can not be defined without value objects
  • πŸ”¬ Harder to test

    They are harder to test, because these objects represent part of your business logic

Conclusion

Entity objects are a pretty neat concept. Even if they are harder to test, they are a powerful tool to represent your business logic. This is a powerful concept to make the code more understandable, maintainable and robust in the long run.

All rights reserved Β© Niels Theen