Implementing Generic Repository Methods

In this lesson, we're going to implement generic repository methods

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:08] We have written our generic repository interface and also added the service to our startup class. Now we need to do the implementation.

    [00:09 - 00:19] If we look at the other repositories, we know that we want to return a list and an entity. But we also have the include statement to think about.

    [00:20 - 00:32] We need to know the type before using the include statement because we don't know about the navigation property when we are using the generic types. We will focus on it in this section.

    [00:33 - 00:40] For now, let's think about returning the entity individually and a list. Let's write the implementation method now.

    [00:41 - 00:53] First of all, we need to inject our store context. So let's create a constructor and use our store context here.

    [00:54 - 01:02] To context and we will simply call it context. Now we need to initialize the field from parameter.

    [01:03 - 01:09] Now let's go to the get ID by async method. Now we will get rid of this.

    [01:10 - 01:27] And since we want to use an asynchronous method, we will write async here and here we will write return away and context dot. This is the place where we want to set the entity that we are going to implement.

    [01:28 - 01:36] We don't know at this time. So we can write set of type T.

    [01:37 - 01:42] And we want to implement the same find async method. So I will pass the ID here.

    [01:43 - 01:52] This tells you an error because type T here must be a reference type. So what we can do is we can say that this T type should always be a class.

    [01:53 - 02:04] So what we can do is here, we can write where T is a class. This means simply dynamic type here must always be a class.

    [02:05 - 02:10] Let's implement the second method now. First of all, let's write async here.

    [02:11 - 02:19] And let's get rid of this line. And now we will say return await context dot set.

    [02:20 - 02:28] Same thing type T and two list async method. We don't have it.

    [02:29 - 02:36] So we'll have to import it using Microsoft entity framework call. I hope you understand what we are doing here.

    [02:37 - 02:55] The type T is dynamic and it will be replaced with the actual entity when we will call and use these methods. Comparing it to the other repository, we cannot use include statement here because we simply don't know the entity type, which means we don't know about their navigation properties either.

    [02:56 - 03:02] So in this lecture, we will return the main entity first. We will talk about the navigation properties in the future lessons.

    [03:03 - 03:08] We just have to go to the controllers now and use this repository. Let's close these first.

    [03:09 - 03:16] I can close all of it. And I can go to the courses controller.

    [03:17 - 03:40] I will replace this course repository with the generic repository that we call generic and I will remove this and also this repository and before initializing it. Now in the generic repository, we always mentioned the dynamic type T, but now we know the entity right.

    [03:41 - 03:51] We know that we want to return the cost type. So what we'll do is generic repository of type costs and I will initialize field from parameter.

    [03:52 - 04:09] We see errors because the name of the methods have changed as well. So let's get rid of this and if we write repository dot, we will get the list all async method, which we have created and get course by ID method is now.

    [04:10 - 04:20] We will automatically see it. Let's remove this and this is the get by ID async method and I will face the ID .

    [04:21 - 04:29] This time we have mentioned the type cause. So P will automatically change to cause and it will return the courses.

    [04:30 - 04:41] Let's implement the same thing in our categories controller. Let's go to categories controller and replace category repository with generic repository.

    [04:42 - 04:57] Let's get rid of this and this as well and generic repository is of like category. Let's initialize field from parameter.

    [04:58 - 05:22] We need to replace get categories asin method with list all async method and get categories by ID method with get by ID asin method. We are doing the same thing now, but we have one interface and one repository instead of two.

    [05:23 - 05:34] Not just that, we can reuse them for all the methods that do the same kind of job. What we will do is open our terminal and run our server.

    [05:35 - 05:39] That will run dot net. Watch.

    [05:40 - 05:47] Run. Open postman and run all these queries again.

    [05:48 - 05:56] We have all the courses. That's when get cause request, but we don't see the requirements, learnings and category.

    [05:57 - 06:00] This is something we are going to fix in the future lesson. So don't worry about it.

    [06:01 - 06:09] Our main focus was to return the main entity, which we can write now. Also, let's call get categories method.

    [06:10 - 06:20] This should be no problem because we are not calling any navigation property here, but inside get category method, we won't see any process. So this is something we are going to fix.

    [06:21 - 06:26] So don't worry about it. Now in the next lesson, let's see how to include the navigation property.