DDD during lunchbreak

Repository

Introduction

In this article, we'll delve into the world of Domain Driven Design (DDD) during your lunch break and demystify the concept of Repositories. We'll explore what a Repository 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 the Repository

The repository pattern is a design pattern that provides an abstraction layer between the domain model and the data access layer. It acts as a collection of objects, providing a way to retrieve, store, and manage domain objects.
Here is an example of a repository in PHP:

            class UserRepository
            {
                public function __construct(Database $database)
                {
                    $this->database = $database;
                }

                public function save(User $user): string
                {
                    $sql = 'INSERT INTO users (first_name, last_name) VALUES (?, ?)';
                    $this->database->execute($sql, [$user->firstName(), $user->lastName()]);
                    $this->database->save($user);
                }

                public function fetchById(UserId $id): User
                {
                    $sql = 'SELECT * FROM users WHERE id = ?';
                    $data = $this->database->fetch($sql, [$id->toInt()]);
                    return new User($data['first_name'], $data['last_name']);
                }

                public function fetchByName(Name $name): User
                {
                    $sql = 'SELECT * FROM users WHERE first_name = ? AND last_name = ?';
                    $data = $this->database->fetch($sql, [$name->firstName(), $name->lastName()]);
                    return new User($data['first_name'], $data['last_name']);
                }
            }
        
In this example, the UserRepository class is a simple implementation of a repository. It contains methods for saving and fetching user objects from a database. The save method inserts a new user record into the database, while the fetchById and fetchByName methods retrieve user records based on their ID and name, respectively. Did you notice that only used entities here? No arrays, no stdClass, no mixed types. Just entities and value objects. That makes the code pretty robust and easy to understand. There is basically no way to put invalid data into your database. If your entity or value object is valid, the data in the database will be valid too.

Characteristics of a repositories

A repository should contain the following characteristics
  • Abstraction of data access
  • Encapsulation of data access logic
  • Centralized data management
  • Separation of concerns
  • Domain-specific operations

Conclusion

Repositories are pretty nifty. They provide a clean and structured way to interact with your data layer, abstracting away the complexities of data access. By just using entitites and value objects, you have a easy way to create robust and good understandable architecture.

All rights reserved © Niels Theen