Architecture: repository pattern
This course examines the repository pattern in a Rails application by measuring what the codebase already has. It covers the difference between a shape boundary and a persistence boundary, the four promises the pattern makes, and how the Active Record and Data Mapper patterns differ. It looks at query objects and what they return, service objects on the write side, and twenty-two ActiveRecord calls found in controllers. It closes with four situations where a repository pays for itself and a short list of concrete changes.
What you'll learn
- ✓The difference between a shape boundary and a persistence boundary
- ✓What the repository pattern promises and how each promise fares over ActiveRecord
- ✓Why never to return a relation, and when to return a Data object or plain data
- ✓What service objects own on the write side, and the aggregate problem
- ✓A rule for which ActiveRecord calls can stay in controllers
- ✓Four situations where a repository is worth its cost
The boundary that already exists
The repository pattern is a way to keep data access out of everything else. Before judging it, measure how much of that this application already has. The numbers below were taken on 2026-09-28.
Read the full lesson free →Curriculum
7 lessonsThe boundary that already exists
Measures query objects, views and controllers, and separates a shape boundary from a persistence boundary.
What the pattern promises
Reviews the pattern's four promises and why stacking Data Mapper on Active Record causes friction in Rails.
The read side
Compares returning a relation, models in a Data object, or plain data, and states a rule for each.
The write side
Shows what service objects already handle on the write side and the aggregate problem repositories face.
The controller exceptions
Sorts twenty-two controller calls into five groups and derives a rule for what controllers may do.
When a repository earns its keep
Names four situations where a repository is the right choice and lists what it costs.
Putting it together: two calls
Summarizes the measurements and the recommendation: move two cross-context calls and write down two rules.