Introduction
In this article, I am going to show you how to use the repository pattern while interacting with Sqlserver through EntityFramework.
Also, I will show how to use the repository pattern in your API controller which can be exposed at the UI client level for making the Blazor WASM user component.
First of all, I am back in C# Corner after 10 years with more experience and focus.
So now let's start. As we all know the Repository pattern are classes or components that encapsulate the logic required to access data sources.
They centralize common data access functionality, providing better maintainability and decoupling the infrastructure or technology used to access databases from the domain model layer. It gives you the flexibility to make your architecture more loosely couple for the TDD approach.
So now let's start a BLAZOR WASM Application using vs2022 (I am using vs2022)
Step 1 - Create Project

Use the below setting
![]()
Now you have the below 3 Project Settings (My application name is “BlazorApp1)
![]()
Now before working in server layer we need to create the Model class under “Shared” Project which can be reuse at Server and Client layer later
Step 2
Let’s create the two below classes 1.Grade.cs 2. Student.cs under the “Model” folder
![]()
Grade has 1 to many relationships with Student
We have created the model class as a Domain driven Development approach so that it will create the relationship table after running the migration
Step 3
Now work in Server Layer
First of all, create the DB context class under the “Data” folder which holds our POCO class Student and Grade
![]()
Step 4
Entry Connection string at “appsettings.json”
![]()
Step 5
Add dependency Injection for this connection string at “Program.cs”
![]()
Step 6
Now execute the migration from the npm packet manager
- Add-Migration “TableCreate”
- Update-Database
After executing the above command the below things will happen for the Migration
![]()
Now you have the table creation at SQL DB
![]()
Step 7
Now let's create the Repository pattern by creating first the “Repository” folder
Under this folder create the below 2 interface
![]()
So in our Interface for the Student and Grade we have our necessity contract which need to be implemented.
So we have created a “manager” Folder where we will implement our interface by using dependency injection for the DB context class which will interact with
Our table data
![]()
Step 8
So by using repository pattern we have created our DAL layer now we can create our API controller and can call the repository instance for accessing our dto object
Before that we need to add our managerial class dependency injection at “Program.cs” class
![]()
Now let's create the Grade and Student API Controller which we will expose at Client-side UI operation
See in the controller we have constructor dependency injection for our Repository Interface class and from there we are accessing our DAL Managerial class for both Student and Grade manager DAL class layer.
In our next article, I will show you how to use this API controller at the Client UI side for the data operation. Till then happy coding 😊