Skip to main content

What is a Repository?

A Repository is an abstraction that mediates between the domain layer and the data mapping layer. It provides a collection-like interface for accessing domain aggregates while hiding the complexity of database operations.

Why Use Repositories?

Domain Isolation

Domain code stays free of persistence concerns

Testability

Easy to mock or use in-memory implementations for testing

Flexibility

Switch databases or ORMs without changing domain code

Consistency

Centralized place for query logic and data access patterns

Repository Types

The library provides several base classes for different use cases:

Repository (Full CRUD)

Complete read and write operations:

ReadRepository

Read-only operations (useful for CQRS read models):

WriteRepository

Write-only operations:

Implementing a Repository

Step 1: Define Your Aggregate

Step 2: Create Mappers

Mappers transform data between domain models and persistence format:

Step 3: Implement Repository

Here’s a database-agnostic implementation skeleton:
For complete implementations with specific ORMs, see:

Using the Repository

Finding Entities

Saving Entities

Deleting Entities

Custom Query Methods

Add domain-specific query methods to your repository:

Repository with Change Tracking

For aggregates with complex nested structures, use change tracking to efficiently persist only what changed:
The Prisma Adapter provides PrismaBatchExecutor that handles all this automatically.

Testing with Repositories

In-Memory Implementation

Create an in-memory implementation for unit tests:

Using in Tests

Next Steps

Criteria

Learn about type-safe query building

Mappers

Domain ↔ persistence data transformation

Schema Registry

Map entities to database tables

Prisma Integration

Complete Prisma implementation