What is Repository Pattern in .Net Core - Repository Pattern C#

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
In this video, we'll explore how to apply  repository patterns in dotnet core. I'll be demonstrating the implementation with a Blazor web  app. Although the same approach can be applied to any dotnet core applications. Here, I have created  two entities. The initial entity pertains to product categories and contains some properties.  This collection of products property facilitates foreign key mapping. The second entity is the  product which includes properties such as name, price and product category ID mapping. Next, we'll  proceed to develop repository classes for these entities. It's important to note that, you can  incorporate as many entities as necessary for your project. The source code for this project  is available on my patreon page. Join my patreon to access exclusive benefits. Moving forward, we'll  establish a folder named repositories. Subsequently, we'll generate a new interface called  IProductCategoryRepository with in this designated folder. Adopting the practice of implementing  services with interfaces is considered a best practice. This approach encourages loose coupling  facilitating seamless replacement or extension of implementations without disrupting the overall  functionality of the application. In this interface, will outline the necessary methods for the  product category repository. The initial method will retrieve all the product categories and will  be named "Get". The subsequent method is designated for fetching a product category based on its  unique identifier. Following that, we'll define a method for creating a new product category.  Subsequently, another method will be implemented for updating existing product categories. Lastly,  we'll include a method specifically for deleting product categories. We'll proceed by creating a  class called product category repository within the same folder. Initially, we'll inherit the  class from the previously defined interface. Implementing the interface will automatically  include the required methods in the class. Before starting the method implementation, we'll declare a  private readonly object of the DbContext and assign it within the class Constructor. In the create  method, we utilize the AddAsync method to save the product category. Additionally, we ensure  changes are persisted using the SaveChangesAsync method. For the delete method, we remove  the designated product category and subsequently save the changes. The "Get" method retrieves all the  product categories and returns them as a list. To fetch a product category by its unique identifier,  we filter based on the ID and returns the result. It's important to note that, this method's return  type should be nullable to accommodate situations where no record matches the provided ID. Both  here and in the interface. Finally, in the update method we implement the necessary steps to modify  the product category and ensure changes are saved thereafter. With these implementations the product  category repository is now fully operational. Moving on to the product repository, we'll begin  by establishing the interface named IProductRepository. If you are curious about how interfaces  facilitate easy replacement of implementations you can refer to our video on .NET Core Dependency  Injection, where I have demonstrated this specific topic. Similar to the product category repository  interface, here I will also define the following methods. Firstly a "Get" method for retrieving  all the products. Secondly, another "Get" method for fetching a product by its unique identifier.  Following that, methods for create, update and delete operations will be defined. We'll proceed by  creating the product repository class. This class will inherit from the newly created interface  and we'll proceed to implement its methods. Before diving into the method implementations, we'll  acquire the DbContext service in the Constructor and assign it to a private readonly object. Let's  begin by implementing the "Create" method to add the product and save the changes. Next, we'll implement  the "Delete" method to remove the product and save changes accordingly. In the "Get" method we'll  retrieve all the products and return them as a list. In this "Get" method, will include a filtering  mechanism based on the product ID to return the corresponding product object. Lastly, we'll  implement the "Update" method to modify the product details and ensure changes are saved. With these  implementations, both repositories are now complete. Let's proceed with dependency injection in the  program.cs class. Initially, we'll add the product category repository as a scoped service. Following  the same approach, we'll also add the product repository. With these Services configured, we can  now implement the CRUD functionalities in our application, I won't delve into the design aspects  and Blazor components in detail in this video. You can explore more about Blazor in our Blazor  tutorial series. The playlist link is available in the description. So I have done the implementation  and let me show you the product category page. As you can observe, I have injected the product  category repository interface. In the code section, you'll notice that I have utilized the get, create, update and delete methods of our product category repository. Now moving on to the product page where  I have injected both product category repository and product repository interfaces. Since I'm  displaying the product category selection option here the product category repository  interface is also required. I'll demonstrate it shortly. In the code section, we are making use  of the get, create, update and delete methods of the product repository. Let's execute the project  and observe how the application functions. In the product category page, we'll attempt to add a new  category. Category successfully added. Let's add one more. Now let's try editing the description of  the first category. I'll provide some description and the update is successful. Next, I'll attempt  to delete the category. The deletion process is also functioning as expected. Let's add one more  category to ensure we have enough to display in the products page. Let me change the name of  Category 3 to category 4. Now in the product page, you'll notice the categories we added are available  in the select options. Let's attempt to add a new product. The product is saved successfully. Now  let's try adding one more. Next we'll attempt to edit the product. The changes are successfully  saved. I'll proceed to delete this product and we can see all the functionalities are working as  expected. Don't forget to visit our Patreon page where you can access the source code and enjoy exclusive  benefits. I hope the concept of repository pattern is now clear to you. If you found this video  helpful, please consider liking, sharing and subscribing to our Channel. Thank You for watching  and I'll see you in the next video. Happy Coding!
Info
Channel: Coding Droplets
Views: 3,257
Rating: undefined out of 5
Keywords: repository pattern c#, repository pattern c# web api, what is repository pattern in .net core, Repository Pattern in dotnet Web API, asp.net core repository pattern, repository pattern in asp net core, asp net core repository pattern, repository pattern in asp.net core, asp net core crud operations example, asp.net core repository pattern tutorial, repository pattern in asp.net core web api, repository pattern ef core, asp.net core repository pattern example
Id: aEhAm5wCZ-Y
Channel Id: undefined
Length: 7min 18sec (438 seconds)
Published: Tue Mar 05 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.