Introducing Repository Pattern

In this lesson, we're going to introduce Repository pattern

Project Source Code

Get the project source code below, and follow along with the lesson material.

Download Project Source Code

To set up the project on your local machine, please follow the directions provided in the README.md file. If you run into any issues with running the project source code, then feel free to reach out to the author in the course's Discord channel.

This lesson preview is part of the The newline Guide to Fullstack ASP.NET Core and React course and can be unlocked immediately with a single-time purchase. Already have access to this course? Log in here.

This video is available to students only
Unlock This Course

Get unlimited access to The newline Guide to Fullstack ASP.NET Core and React with a single-time purchase.

Thumbnail for the \newline course The newline Guide to Fullstack ASP.NET Core and React
  • [00:00 - 00:10] In this lesson, we are going to learn about the repository pattern. The repository design pattern in C# is one of the most used design patterns in real-time application.

    [00:11 - 00:17] So what does it do? Its main responsibility is to decouple business code from the data access code.

    [00:18 - 00:26] Right now our store context is doing the exact same thing, but we have to import it in our controller. But we don't want that.

    [00:27 - 00:36] We are going to have more controllers as well rather than just the cost controller. So we can remove the duplicacy here by using a repository pattern.

    [00:37 - 00:47] And it's always good to keep our controllers clean and light. Like entity framework is giving us separation of concerns by making a connection to the database.

    [00:48 - 00:55] Repository pattern gives us separation of concerns from the database context. So it provides us one more layer of abstraction.

    [00:56 - 01:01] It also helps us avoid writing the duplicate code. For example, we have five different controllers.

    [01:02 - 01:10] And somehow all the controllers need to fetch the list of courses. Without repository pattern we'll have to write the same logic five times.

    [01:11 - 01:25] But if we are using repository pattern, we can write it once and import it in all five different controllers. Not just that, repository pattern makes testing of the code much more smart and efficient.

    [01:26 - 01:35] It's much easier to test the method inside repository pattern rather than testing our store context. Obviously it's not easy to understand it right away.

    [01:36 - 01:45] But the more you work with repository pattern, the more you get familiar with its advantages. Well, there are developers that criticize the repository pattern.

    [01:46 - 01:51] According to them, it's too much abstraction of the code. Because entity framework is already in abstraction.

    [01:52 - 02:02] Using repository pattern and entity framework in the same project means abstraction away from abstraction. I don't deny it, but it always comes to the personal choice.

    [02:03 - 02:07] It's one of the most popular pattern. So you can imagine there are more goods than bads.

    [02:08 - 02:24] If we don't use repository pattern, our request is first received by the controller. Our controller then forwards the request to the DB context, which is our store context, which converts the query into SQL query and interact with the database.

    [02:25 - 02:43] Although there's nothing wrong with it, but if you're using multiple controllers, it's always good to use a repository pattern. With repository pattern, the controller forwards the request to the repository, repository then forwards the request to the DB context, which finally interacts with the database.

    [02:44 - 02:50] It's perfectly fine if you don't understand everything. Once we start writing the code, things will get easier to understand.

    [02:51 - 02:53] So let's start implementing it from the next lesson.