Repository — why isolate the data source?
The point
A Repository is a protocol that answers "give me the data" and hides where it comes from: network, in-memory cache, SwiftData/Core Data, a file. The rest of the code (use cases, ViewModels) knows only articles()/save(_:) and domain types, but not URLSession, DTOs or the DB schema.
Why:
- Swapping the source — moving from REST to GraphQL or from Core Data to SwiftData does not touch screens.
- Caching and offline are added inside the repository without changing its clients.
- Tests — a fake repository with an in-memory array instead of network and DB.
- One place for request de-duplication, invalidation, DTO → domain model mapping.
The rest of this answer, with code and interview follow-ups, is in the DevPrep app.
Read the full answer in DevPrep