The Repository Pattern On a Code Base (Domain Driven Approach)

Petros Koulianos ๐Ÿ’€โ˜ ๐Ÿ‘ฝ
3 min readOct 6, 2022

--

Repository Definition, Internals, Usage, In a 3-Layer Architecture

In this article, we will reveal aspects of the repository pattern and how to use it properly on a code base.

This post is originally posted at petran.substack.com

๐ŸŒŠLetโ€™s dive into the main aspects of a Repository

โœ’๏ธ Definition

A Repository encapsulates all the work needed to interact with Aggregate, Entity Models or Value Objects that lives at out-of-process dependencies

Repositories are classes that fetch and manipulate Models that lives out of our code base process such as a Database, a Microservice, a 3rd Party Service, File system IO, etc.

Repositories are boundaries of our code base with the outer world.

A Repository syncs the local models with the state on the source or shows the state of models from the source.

Repository Definition

โš™๏ธ Internals

Repositories have the following responsibilities :

  • Get client criteria
  • Send instructions based on the criteria to the out-of-process dependency (could be more than one)
  • Create a new Object or a collection of Objects with the returned data (Denormalization of the data to a specific Object type or Collection of Objects)
  • Return the new Object or a collection of new Objects to the client
  • Dedicated to return a specific Object Type or an Abstract Type.

For example :

  • A BlogRepository is dedicated to returning only Blog Type Objects
  • A UserRepository is dedicated to returning only Abstract User Objects (AdminUser, CommonUser, etc )
Repository Internals

๐ŸŒฒ Usage

Repository exposes a simple interface to interact with client classes.

Clients must have easy access to Repository instances with the help of DI, Service Containers, and Factories.

With the Repository pattern, we decouple all the work that needs a client to fetch and create objects.

Repositories indicates a place our app communicates with the outer world

Use Repositories in all situations that need to collaborate with external systems.

Repository Usage

๐Ÿ“š Repository In a 3-Layer Architecture

A Repository belongs to the Domain Layer on a 3-Layer Architecture.

Repositories have domain rules and depends on the Infrastructure layer to accomplish their responsibilities such as communicating with a Database or making an HTTP request on a 3rd Party service etc.

Repository at 3 layer Architecture

--

--

Petros Koulianos ๐Ÿ’€โ˜ ๐Ÿ‘ฝ

Software Engineer ๐Ÿ‘ฝ | Building applications for health industry | Work with JavaScript, Typescript, PHP | My Newsletter๐Ÿ“ฉ at petran.substack.com