Key ideas & timestamps
Timestamp links open the original conversation at that moment.
Repository Pattern Fundamentals
Repositories Mediate Between Domain and Data Layers
Rooted in Martin Fowler's architecture patterns, a repository acts as an in-memory collection of domain objects that mediates between the domain and data mapping layers. This abstraction minimizes duplicate query logic across applications and decouples business logic from specific persistence frameworks.
Persistence Frameworks Evolve Constantly Over Time
Persistence frameworks shift approximately every two years, transitioning through technologies like ADO.NET, LINQ-to-SQL, early Entity Framework, NHibernate, DbContext, and Dapper. Using a repository pattern gives applications the architectural freedom to adopt new persistence mechanisms with minimal disruption to the codebase.
Architecture and Implementation Rules
Repositories Exclude Database Update and Save Semantics
Repositories should mimic in-memory collections, containing methods for adding, removing, finding, and retrieving objects by ID. They intentionally exclude update and save methods because in-memory collections mutate objects directly through reference changes, leaving persistence execution to other components.
Unit of Work Coordinates Multi-Repository Transactions
The unit of work pattern maintains a list of objects affected by a business transaction and coordinates writing changes across multiple repositories. By housing the save mechanism within the unit of work rather than individual repositories, applications ensure atomic updates across related data stores.
Entity Framework DbSets Expose IQueryable Traps
While Entity Framework DbSets and DbContext mimic repositories and units of work on the surface, returning IQueryable forces developers to scatter and repeat LINQ query logic across controllers and services. Encapsulating queries within proper custom repositories prevents these leaky abstractions and maintains clean architecture boundaries.
Pragmatic Software Engineering
Complex architectural patterns such as repositories, dependency injection, and clean layers are unnecessary for short-term proof-of-concept projects with volatile requirements. Engineers should weigh project viability and risk before applying heavy design patterns to prevent wasted development effort.