Design Patterns

The Repository Pattern

Mediate between the domain model and the data mapping layer using a collection-like interface for accessing domain objects.

February 22, 2026
#repository-pattern#design-patterns#ddd

A Repository encapsulates the logic for querying and persisting aggregates, presenting a collection-like interface to the domain layer. The domain asks the repository for objects by identity or specification; it never writes SQL or calls an ORM directly. This decouples the domain from persistence infrastructure and makes it straightforward to test the domain with in-memory fakes.

php
interface OrderRepository
{
    public function findById(OrderId $id): ?Order;
    public function findPendingOlderThan(\DateTimeImmutable $cutoff): array;
    public function save(Order $order): void;
    public function remove(Order $order): void;
}
Repository interface in the domain layer

Continue reading

The Repository Pattern | Architecture Hub