Topic 048 / Core
Data access patterns: repository and unit of work
Chapter 4.3 · Design Patterns and Data Access
What and why
Domain logic that calls the database directly — raw SQL or ORM calls
scattered through business methods — couples business rules to a specific persistence
technology and forces every test to stand up a real database. Repository is a
collection-like interface (findById, save, delete) that hides persistence behind a
domain-shaped contract. Unit of Work tracks every change made during one business
operation and commits them together, so one business operation is one atomic unit of
persistence regardless of how many repositories it touches. Builds on: → 47 (repository
and unit of work are themselves patterns from the structural/behavioral vocabulary).
Diagram
TaskService
│ uses
▼
TaskRepository (interface: findById, save, delete — shaped like the domain, not SQL)
│ implemented by
├── PostgresTaskRepository (production)
└── InMemoryTaskRepository (tests — no database)
UnitOfWork.begin()
→ TaskRepository.save(task)
→ AuditRepository.save(entry)
→ UnitOfWork.commit() (both writes land together, or neither does)
How it works
- Define the repository interface in the domain layer, shaped like what the domain
needs (
findActiveTasksForUser), not like SQL. - Implement it once per storage technology; the domain never imports the database driver.
- For an operation touching more than one table (completing a task and writing an audit entry), open a Unit of Work, perform both repository calls against it, then commit once.
- On failure anywhere inside the unit, roll back everything — partial writes never land.
- In tests, swap the real repository for an in-memory fake implementing the same interface; the domain logic runs unchanged.
Practical example
(illustrative). Completing a task in the task manager writes the
Task row and an audit-history row (→ 59) in one operation. Without a Unit of Work, a
crash after the task update but before the audit insert leaves an inconsistent history
with no record of who completed it; with one, both writes commit together or neither does.
When to use it
- Use when: the domain needs a test seam or might outlive one storage technology, or an operation spans multiple tables and needs all-or-nothing persistence.
- Avoid when: the ORM's own session/change-tracking is already an adequate unit of work and interface — a repository layered on top of it is a pass-through wrapper that duplicates the ORM's own API for no benefit.
- Failure scenarios: a "leaky" repository whose methods return query-builder objects, so callers still depend on the underlying database technology despite the interface.
- Simpler alternative: call the ORM/session directly for a small CRUD application with one storage technology and no need for a test double.
Practice
(20–30 min; no lab services — any language, an in-memory map is enough).
Implement TaskRepository with save/findById backed by a plain in-memory map;
implement a UnitOfWork that commits a task update and an audit-entry insert together.
First run it without the Unit of Work and force a mid-operation failure to see the
partial write; then add the Unit of Work and show the same failure leaves neither write
behind. Done when the partial write is reproduced without the Unit of Work and impossible
with it.
Key takeaway
Repository isolates the domain from storage technology; Unit of Work makes a multi-write business operation atomic. Done when you can: swap a repository's implementation without touching the domain, and show an all-or-nothing commit survives a mid-operation failure. Used next in → 69 (ACID — the guarantee Unit of Work relies on), → 71 (concurrency control under concurrent writes); Project 1.
References
None — this topic makes no external claims.
Source directory