Domain-Driven Design, part 5 — Repository

Collections and Reality

Let’s imagine that we have a system which runs continually, has enough memory and which is for a single user only. With this kind of system, we can have all objects in memory collections and everything is shiny. Memory collections are enough — they allow us to store, receive and remove objects.


Whole idea is about that we keep pretending that we have collections. Clever and persistent collections, so-called repositories. The domain layer would seem to live continually.

Persistence Responsibility

The repository can be responsible for persisting objects. It would make some sense to have a saving method that instantly persists an object.

Concrete Implementation

We can store domain objects in a relational database, in a document database, in an external system connected by API or in anything else we can imagine. All these systems are infrastructure for the domain so the repository implementation is in the infrastructure layer.

Inversion of Dependency

In an active record persistence pattern and similar patterns, the domain depends on the infrastructure. But we have created a domain repository interface and the implementation is the infrastructure. The domain layer is still independent. For those who like SOLID, everything smoothly fit together.

Memory Implementation

The easiest and the most instructive is a memory implementation. An implementation which just keeps objects during a life and does not persist them at all.

Interface Test

We can write tests for the memory implementation and when we integrate a database storage, we can write tests for the database integration. But these tests can be pretty similar, so we can think of testing the repository interface only.


Repositories are persistent collections and allow us to pretend that the system is in-memory. The repository works with complete aggregate and is an interface in the domain layer. The concrete implementation is done by infrastructure which we use. Tests can be written for the repository interface.


CLEMSON, Toby. Testing Strategies in a Microservice Architecture [online]. 2014 [2018–01–11]. Available:

Complete code


Are you designing architecture and like DDD approach? Hire me, I can help you —



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store