The Repository Pattern, according to Martin Fowler, provides
a "layer of abstraction over the mapping layer where query construction
code is concentrated" to "minimize duplicate query logic." In
practice it is usually a collection of data access services, grouped in a
similar way to the domain model classes.
By accessing repositories via interfaces, the repository
pattern helps to break the dependency between the domain model and data access
code. This is invaluable for unit testing because the domain model can be
I implement the repository pattern by defining one
repository class for each domain model entity that requires specialized data
access methods (other than the standard create, read, update and delete). If an
entity does not require specialized data access methods then I will use a
generic repository for that entity. A repository class contains the specialized
data access methods required for its corresponding domain model entity.
The following class diagram shows an example implementation
with two domain entity classes, Shape and Vertex. Shape has a specialized
repository (IShapeRepository). Vertex does not have a specialized repository,
so it will just use the generic repository (IRepository<Vertex>).