Design Pattern: Repository | Complete and simple explanation
Tags: programming, productivity, designpatterns
Published: 2023-05-17 17:58:41

## 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

## <u>Theory</u>
### Logic
**"Design pattern"**
- **Pattern**: is a type of repetitive events or objects
![Design Pattern Repository](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/a8hizh9zs3bzgaimxs1n.png)
- **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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/g6siycrdrn3q3c5fdkvp.png)


- 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.

## <u>Practice</u>
### 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

**<u>Method: no repository design pattern
</u>**
![No repository design pattern](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jaw26wm3ph7hnbnn97ki.png)



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

**<u>Method: no repository design pattern</u>**
![With repository design pattern](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4ozmk2i9svzl6y7x31c7.png)

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](https://twitter.com/danibarazarteg), [GitHub ](https://github.com/danielabarazarte) and [LinkedIn](https://www.linkedin.com/in/danielabarazarteg/)

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