Skip to content

Repository Pattern

Mike Hanson edited this page Feb 25, 2018 · 1 revision

From MartinFowler.com

A Repository mediates between the domain and data mapping layers, acting like an in-memory domain object collection. Client objects construct query specifications declaratively and submit them to Repository for satisfaction. Objects can be added to and removed from the Repository, as they can from a simple collection of objects, and the mapping code encapsulated by the Repository will carry out the appropriate operations behind the scenes. Conceptually, a Repository encapsulates the set of objects persisted in a data store and the operations performed over them, providing a more object-oriented view of the persistence layer. Repository also supports the objective of achieving a clean separation and one-way dependency between the domain and data mapping layers.

SqlRepo implements the Repository Pattern via a Repository Factory that will provide a Generic Repository for a specified root type. Once you have a repository you can use it to create and build SQL Statements using Lambda Expressions and execute them. The results from the SQL execution are then mapped into domain entities and returned to calling code as a collection or single object.

We believe this is a valid implementation of the Repository Pattern even though it does not implement the "in-memory domain object collection" aspect mentioned above. SqlRepo allows you to "construct query specifications declartively and submit them to the Repository for satisfaction" and it does this in a strongly typed testable fashion elminating the need to embed SQL strings in code.

Clone this wiki locally