DEV Community

Cover image for Design Pattern: Repository | Complete and simple explanation
Daniela "Ingeniela" Barazarte
Daniela "Ingeniela" Barazarte

Posted on • Edited on

Design Pattern: Repository | Complete and simple explanation

Introduction

Hello, my name is Daniela Barazarte and I want to welcome you to this complete explanation of the repository design pattern. This explanation will be so simple and direct that even a 5-year-old child could understand it.

If you prefer videos, here is a complete tutorial made by me on YouTube: https://www.youtube.com/watch?v=b2tPRbQJing

Theory

Logic

"Design pattern"

  • Pattern: is a type of repetitive events or objects Design Pattern Repository
  • Design: creative activity whose purpose is to project objects that are useful and aesthetic

"Repository"

  • Repository: warehouse or place where certain things are stored

Simple definition

Design patterns refer to the idea that there are common, reusable patterns or solutions that can be applied to repetitive problems in software design. It is called "pattern" because it is repeated in several projects, and design because it projects objects in a simple way.

The repository is a software design pattern that is used to separate the application (programming elements, such as classes, interfaces, methods) from the data storage (database) and functions as a mediator between the two.

The repository provides a set of methods to perform CRUD (Create, Read, Update, Delete) operations on the data.

Structure

Repository with vs without

  • The top of this image represents a system without the repository design pattern
  • The bottom part of this image represents a system with the repository design pattern

As you can see, the repository design pattern helps to connect the application logic (of having the CRUD methods and functions) with the database... and when the repository is not there, the connection is direct. The fact that the repository is present as an intermediary is important and you will be seeing why in this blog.

Practice

Exercise

You have a WebAPI application that has methods to create, read, edit, and delete tickets for a train station… have those tickets stored in a SQL database.

There are two methods to solve the task:

  1. Do it without the repository design pattern
  2. Do it with the repository design pattern

Method: no repository design pattern

No repository design pattern

The database storage methods would be together with the ticket methods (create, read, edit and delete tickets).

Method: no repository design pattern
With repository design pattern

Database storage methods are now separate and not next to ticket methods

Importance

The repository design pattern has several benefits:

  • Separation of responsibilities: The Repository design pattern helps to separate the data access logic from the business logic of the application.
  • Abstraction: The Repository design pattern provides an abstraction layer between the application and the data storage mechanism. This abstraction layer makes it easier to switch to a different data storage mechanism without affecting application code that uses the repository interface.
  • Encapsulation: The Repository design pattern encapsulates the data access logic in one place, making it easier to manage and modify.
  • Testability: The Repository design pattern makes it easy to test your application's business logic by providing a mock repository implementation that can be used for unit testing.
  • Scalability: The Repository design pattern can help improve the scalability of an application by providing a centralized place to manage data access, which can help reduce contention and improve performance.

Thanks

Thank you very much for reading, if you have any questions you can comment, you can also find me on my other networks like Twitter, GitHub and LinkedIn

I'll be preparing a blog post on how to implement Repository in C# and .NET so stay tuned <3

Top comments (0)