Repository Design Pattern

What is Repository Design Pattern

Repository is just a class that is associated with some entity and it manages all possible actions of that entity including CRUD operation.

For example we have a Student entity then , a Student repository will contain these basic methods e.g. Add , Update , Get , GetById and Delete

The Repository Pattern is a layer that mediates between the domain and the data access layers. Itโ€™s like a buffer that controls how data is accessed and manipulated

Then we inject our repositories in services/handlers and use them [ But not all the time ]

How to implement it in .NET Core

Letโ€™s suppose we are dealing with Student entity. So its repository would look like this.

public interface IStudentRepositroy
{
    public bool Delete(int id);

    public bool Add (StudentDTO student);

    public bool Update (StudentDTO student);

    public Task<StudentDTO> GetById(int id);

    public Task<List<StudentDTO>> Get(int page, int pageSize);
}

With implementation:

So the main thing to note here is that only repositories are responsible to talk with database.

Donโ€™t forget to register dependency of your repository in your Program.cs

We are creating a layer of abstraction here , handler has no idea how those method are working and what database is being used behind the seen.

It is common practice to use generic repositories where we have to deal with multiple CRUD operations for multiple entities

Suppose we are making a Land System where we have to deal with States, Cities ,Districts and next in the hierarchy , then generic repository would be perfect solution for that problem.

Dependency injection of open generics is little bit different , check this from a previous post :

Unit of Work

I have above mentioned that we can inject our repositories in constructor of our handler or service and use it . This is fine just for couple of repos what if those increases then this is not good approach to keep injecting every new repo in constructor.

That is where Unit of Work pattern comes into an action. We put all of our repositories in UnitOfWok and it is responsible for pushing changes in database as well. That is how it looks like :

UnitOfWork would be like this :

Now instead of injecting repositories we would inject our IUnitOfWork , this approach has following benefits

More clean

It saves use from multiple injections in our desired class/services/handlers

It saves us from data leakage if multiple repositories objects have different instance of DbContext

Now we can use IUnitOfWork in our service/handler/controller like this

Controller

Pros and Cons

These are few benefits of using this pattern

  1. ๐—ฆ๐—ฒ๐—ฝ๐—ฎ๐—ฟ๐—ฎ๐˜๐—ถ๐—ผ๐—ป ๐—ผ๐—ณ ๐—ฐ๐—ผ๐—ป๐—ฐ๐—ฒ๐—ฟ๐—ป๐˜€: The repository pattern separates the logic that retrieves the data and maps it to the entity model.
  2. ๐—ฅ๐—ฒ๐—ฑ๐˜‚๐—ฐ๐—ฒ๐˜€ ๐—ฐ๐—ผ๐—ฑ๐—ฒ ๐—ฑ๐˜‚๐—ฝ๐—น๐—ถ๐—ฐ๐—ฎ๐˜๐—ถ๐—ผ๐—ป: Your data access logic can be reused across multiple projects.
  3. ๐—จ๐—ป๐—ถ๐˜ ๐—ง๐—ฒ๐˜€๐˜๐—ถ๐—ป๐—ด: It becomes a breeze as the data access logic is decoupled from the business services.
  4. ๐—›๐—ถ๐—ฑ๐—ฒ๐˜€ ๐—ฆ๐—ค๐—Ÿ: Hides complex SQL or query language details, offering a cleaner API for accessing data.

But no hero is without its flaws:

  1. ๐—ข๐˜ƒ๐—ฒ๐—ฟ๐—ต๐—ฒ๐—ฎ๐—ฑ: Implementing the repository pattern introduces a new layer, which can be an overhead for small projects.
  2. ๐—š๐—ฒ๐—ป๐—ฒ๐—ฟ๐—ถ๐—ฐ ๐—ฅ๐—ฒ๐—ฝ๐—ผ๐˜€๐—ถ๐˜๐—ผ๐—ฟ๐—ถ๐—ฒ๐˜€: They can limit your ability to perform complex database operations and can lead to performance penalties.
  3. ๐—Ÿ๐—ฒ๐—ฎ๐—ฟ๐—ป๐—ถ๐—ป๐—ด ๐—–๐˜‚๐—ฟ๐˜ƒ๐—ฒ: It takes time to understand and implement the repository pattern correctly

Covering each aspect of this topic in less than 5 minutes was difficult , I have tried to give basic overview that how it looks like using a repository pattern. For more detailed read visit Microsoft Docs

This article was originally published at https://mwaseemzakir.substack.com/ on .

Whenever you're ready, there are 3 ways I can help you:

  1. Subscribe to my youtube channel : For in-depth tutorials, coding tips, and industry insights.
  2. Promote yourself to 9,000+ subscribers : By sponsoring this newsletter
  3. Patreon community : Get access to all of my blogs and articles at one place