11 of 22 · System architecture
Repository
A repository gives the rest of the app a collection-like API for domain objects, such as notes() and save(_:), and hides where the data comes from: memory, disk or network.
How it's wired
- ThanksRepository reads cache → disk, and writes disk first, then cache, then syncs remote.
- Callers just call notes() and save(_:).
| Part | Owns | Looks like |
|---|---|---|
| Repository | Collection-like API, source-of-truth policy | actor ThanksRepository |
| Local store | Fast reads, offline | Memory cache, file, Core Data |
| Remote | Server of record or sync target | RPC / HTTP client |
The questions it has to answer
- Source of truth. Offline-first: local is truth and the network is sync. Online-first: the server is truth and local is a cache. Mixing both without a stated policy produces conflicting writes.
- Staleness. Pick an invalidation policy: TTL, server ETags, or push invalidation.
- Conflicts. Two devices edit the same note. Last-write-wins is simple and loses data; per-field merges or server versioning cost more.
- Concurrency. An actor serializes access, but awaits interleave: two saves can reach disk in one order and the cache in another. Track in-flight writes per id, or version each record and keep the newest.
- Don't leak storage types (managed objects, DTOs) through it. Return domain structs.
When I'd add a repository
| Data from more than one source | Cache plus disk plus network. |
|---|---|
| Offline support | The repository owns the sync queue and the policy. |
| One API call, no caching | A plain service is enough. |
Compared
Online-first vs offline-first
| Online-first | Offline-first | |
|---|---|---|
| Write path | Request, then update cache | Local write, then sync queue |
| Failure mode | Errors on bad network | Conflicts to resolve |
| Fits | Feeds, search | Notes, drafts, messages |
Expert notes
- In a system design round, the repository is where you answer the offline, caching and conflict questions. Say the policy out loud.
Check yourself
Answer out loud before you reveal. Recall is what makes it stick.
Explain Repository in 30 seconds.
- A repository gives features a collection-like API, such as notes() and save(_:), and hides whether data comes from memory, disk or network.
- Its real job is policy: which source is the truth, when caches go stale, and how conflicts resolve.
- For offline-first, local is the truth and the network is sync.
- I'd skip it for a single endpoint with no caching.
What is the Repository pattern, and what does it centralize?
- A Repository sits between the domain and the data-mapping layer and makes stored objects look like an in-memory collection, like
repo.users(matching: criteria). - Callers describe what they want declaratively, and the repository's mapping code does the real fetch.
- Query construction lives in one place, and dependencies point one way.
- It earns its keep with many entities or heavy querying.
- Skip it for a single endpoint with no caching, since it's one more layer to maintain.
You must ship a feature before the storage choice is decided. How do you avoid rewriting the view model later?
- Start with a protocol, like
protocol UserRepository { func find(id: User.ID) async throws -> User? }, plus create and delete. - Inject it into the view model's
init. - Ship with an in-memory actor, then swap in UserDefaults or Core Data later without touching the view model.
- The in-memory version doubles as your test fake.
Similar query logic for the same entities is duplicated across many view models. Where should it live, and why?
- Move it into a Repository.
- Concentrating query construction in one place stops duplicates from drifting apart, and it pays off most with many entities or heavy querying.
- Callers pass declarative criteria and never touch data mapping, so storage details stay behind a one-way dependency.
- For a small app with three simple fetches, it's overkill.
Five view models each call the network singleton directly. How do you restructure access to remote and disk data?
- Give each resource, like network, disk or sensors, one model controller and inject it.
- Don't let view models reach
NetworkManager.shared. - View controllers or coordinators hand the controller down, so access paths are visible and mockable.
- The honest trade-off is that it's still one shared object.
- You've traded global reachability for one injected owner you can replace in tests.