Repository Pattern and Unit of Work With ASP - NET Core Web API - by Yohan Malshika - Enlear Academy
Repository Pattern and Unit of Work With ASP - NET Core Web API - by Yohan Malshika - Enlear Academy
of Work with ASP.NET Core Web API | by Yohan Malshika | Enlear Academy
Open in app
Hello All, I plan to tell you how to implement a unit of work with the repository
pattern in ASP.NET Core today. In that case, I'll discuss,
https://enlear.academy/repository-pattern-and-unit-of-work-with-asp-net-core-web-api-6802e1aa4f78 1/24
8/15/23, 1:40 PM Repository Pattern and Unit of Work with ASP.NET Core Web API | by Yohan Malshika | Enlear Academy
Benefits of UoW
How to implement the unit of work with repository pattern in ASP.NET Core
Web API
I already discussed the repository pattern with basic implementation in the below
article.
Create ASP.NET Core CRUD Web API with the Repository pattern
Hi all, Hope all are doing well!. So In the last articles, I showed you
how to create CI/CD pipeline for ASP.NET Web…
malshikay.medium.com
All database actions related to a given entity are defined using the non-generic
repository pattern within a separate class. For Example, suppose we have two
entities (Ex-Student and Employee ). In that case, we will create two repositories for
each entity with the basic CRUD operations and other operations related to each
entity.
The generic repository pattern is used to provide common database operations for
all database entities in a single class, such as CRUD operations and so on. First, I'll
show how to implement the generic repository pattern with the Example.
https://enlear.academy/repository-pattern-and-unit-of-work-with-asp-net-core-web-api-6802e1aa4f78 2/24
8/15/23, 1:40 PM Repository Pattern and Unit of Work with ASP.NET Core Web API | by Yohan Malshika | Enlear Academy
https://enlear.academy/repository-pattern-and-unit-of-work-with-asp-net-core-web-api-6802e1aa4f78 3/24
8/15/23, 1:40 PM Repository Pattern and Unit of Work with ASP.NET Core Web API | by Yohan Malshika | Enlear Academy
The above code example seems to be fine. But the issue will arise when we add
another repository for another entity like this repository. For Example, if we add the
repository for the Customer entity, we have to maintain its instance of DbContext.
Then these repositories will maintain their instance of DbContext. This could lead
to problems if each DbContext has its in-memory list of updates to entity records. In
this situation, if one repository's SaveChanges fails while the other succeeds,
database inconsistency will arise. This is where the UnitOfWork concept comes into
play.
Also, You can see the high-level view of the Unit of Work and Repository pattern in
the above figure.
https://enlear.academy/repository-pattern-and-unit-of-work-with-asp-net-core-web-api-6802e1aa4f78 4/24
8/15/23, 1:40 PM Repository Pattern and Unit of Work with ASP.NET Core Web API | by Yohan Malshika | Enlear Academy
4. Increase the abstraction level and maintain the business logic separately.
5. How to implement the unit of work with repository pattern in ASP.NET Core Web API
First, we need to create an ASP.NET Core Web API project. I am not explaining how
to create it here. Then run it and check the project.
Then we need to install the required packages for use to SQL server and entity
framework core. In that case, you could navigate to Nuget package manager and
then install the required packages into our solution.
Microsoft.AspNetCore.Identity.EntityFrameworkCore
Microsoft.EntityFrameworkCore.Sqlite
Microsoft.EntityFrameworkCore.Tools
Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore
Then remove the boilerplate code that has been created when creating the project
(WeatherForecast.cs, Controller/WeatherForecastController.cs) after we need to
modify the appsettings.json file to add the connection string of our database. So, It's
a small task, and it's up to you.
In here, I follow the code-first approach in EF core to create and manage our
database. In that case, we need to create a Model (Entity) class. So, first, create the
folder called Models in the root directory and then create the Employee model class
inside the Model folder as below code.
https://enlear.academy/repository-pattern-and-unit-of-work-with-asp-net-core-web-api-6802e1aa4f78 5/24
8/15/23, 1:40 PM Repository Pattern and Unit of Work with ASP.NET Core Web API | by Yohan Malshika | Enlear Academy
Then we need to create DbContext for our application. In that case, Create a folder
called Data in the root directory and then create DBContext class which implement
the DbContext as below code and also need to our Employee model class into
DbContext as below code.
https://enlear.academy/repository-pattern-and-unit-of-work-with-asp-net-core-web-api-6802e1aa4f78 6/24
8/15/23, 1:40 PM Repository Pattern and Unit of Work with ASP.NET Core Web API | by Yohan Malshika | Enlear Academy
You can see the DbContext class with the above code. In that code, the
OnModelCreating method provides the ability for us to manage the table properties
of the tables in the database. As well, we add our Employee model class. In that
case, the DbSet property will help to create the table which adds with it by using the
EF Core.
Then we have to create our database. In that case, navigate to the package manager
console of visual studio and then use the following commands to create the
database. First, enter the add-migration to create the migration by adding the
migration snapshot and then enter update-database to create and update the
database by using the last migration snapshot.
https://enlear.academy/repository-pattern-and-unit-of-work-with-asp-net-core-web-api-6802e1aa4f78 7/24
8/15/23, 1:40 PM Repository Pattern and Unit of Work with ASP.NET Core Web API | by Yohan Malshika | Enlear Academy
add-migration initialMigration
update-database
Now we created our database with our ASP.NET Core Web API project.
Now we have to implement the repository pattern. In that case, I follow the generic
repository pattern to create the repository pattern for our project. First, we need to
create a folder called services in the root directory and then create the
IGenericRepository interface inside the service folder as the following code.
https://enlear.academy/repository-pattern-and-unit-of-work-with-asp-net-core-web-api-6802e1aa4f78 8/24
8/15/23, 1:40 PM Repository Pattern and Unit of Work with ASP.NET Core Web API | by Yohan Malshika | Enlear Academy
I explained the Generic repository above. Here, T is a specific class, and the
functions will depend on our requirements and preference. In this case, I added 6
functions that are commonly used on other repositories.
This class will implement the IGenericRepository Interface. The DBContext also will
be injected here. In this approach, all operations connected to the dbContext object
are hidden within Repository Classes. However, we have not yet
committed/updated/saved the database modifications in any way. This is something
https://enlear.academy/repository-pattern-and-unit-of-work-with-asp-net-core-web-api-6802e1aa4f78 9/24
8/15/23, 1:40 PM Repository Pattern and Unit of Work with ASP.NET Core Web API | by Yohan Malshika | Enlear Academy
that should not be done in a Repository Class. We'll discuss that with the Unit of
Work.
Here, We need to inherit IGenericRepository and add the new functions related to
IEmployeeRepository. For Example, we could add the functions called
GetEmployeeAttendance() because it's not a common operation that is not suitable
for the IGenericRepository.
https://enlear.academy/repository-pattern-and-unit-of-work-with-asp-net-core-web-api-6802e1aa4f78 10/24
8/15/23, 1:40 PM Repository Pattern and Unit of Work with ASP.NET Core Web API | by Yohan Malshika | Enlear Academy
Now we have to implement the Unit of Work in our project. Basically, I explained
what unit of work in the above section is. Now we have to learn it with the Example.
In that case, I create the Configuration folder in the root directory of the project
folder. Then we need to create the IUnitOfWork interface inside the configuration
folder as following code.
https://enlear.academy/repository-pattern-and-unit-of-work-with-asp-net-core-web-api-6802e1aa4f78 11/24
8/15/23, 1:40 PM Repository Pattern and Unit of Work with ASP.NET Core Web API | by Yohan Malshika | Enlear Academy
Now we have built one repository with the Generic Repository pattern. We could
inject the repositories that we implement in our project and then could access the
data. But when we have a lot of repositories, we need to inject all of them. It's not
good practice. In that case, we could use the unit of work to wrap all the repositories
into a single object.
The Unit of Work is in control of exposing the relevant Repositories and Committing
Changes to the DataSource in order to ensure a complete transaction with no data
loss.
Now we have to implement this interface. In that case, create UnitOfWork class
inside the configuration folder as the following code.
https://enlear.academy/repository-pattern-and-unit-of-work-with-asp-net-core-web-api-6802e1aa4f78 12/24
8/15/23, 1:40 PM Repository Pattern and Unit of Work with ASP.NET Core Web API | by Yohan Malshika | Enlear Academy
We are injecting the DBContext and ILogger and implementing the CompleteAsync
and the Dispose functions to use when the commit/update/remove operations with
the database were.
Now navigate to startup class and need to register IUnitOfWork interface in our
project as follows.
services.AddScoped<IUnitOfWork, UnitOfWork>();
https://enlear.academy/repository-pattern-and-unit-of-work-with-asp-net-core-web-api-6802e1aa4f78 13/24
8/15/23, 1:40 PM Repository Pattern and Unit of Work with ASP.NET Core Web API | by Yohan Malshika | Enlear Academy
Then we have to implement the controller in our project. In that case, We have to
create the controller called EmployeeController inside the Controllers folder, as
below code.
We inject the IUnitOfWork as the object in this controller. Then we have to use the
unit of work to add the services and logic to the related methods in this controller.
https://enlear.academy/repository-pattern-and-unit-of-work-with-asp-net-core-web-api-6802e1aa4f78 14/24
8/15/23, 1:40 PM Repository Pattern and Unit of Work with ASP.NET Core Web API | by Yohan Malshika | Enlear Academy
Now we have to run this project. And then we could access our ASP.NET Core Web
API as below figure.
So, We run our web API project successfully and If you need the project that we
implement, you could get it with this GitHub Repository.
So That's it for today. I hope you learned something new from my article.
Learn More
https://enlear.academy/repository-pattern-and-unit-of-work-with-asp-net-core-web-api-6802e1aa4f78 15/24
8/15/23, 1:40 PM Repository Pattern and Unit of Work with ASP.NET Core Web API | by Yohan Malshika | Enlear Academy
Follow
https://enlear.academy/repository-pattern-and-unit-of-work-with-asp-net-core-web-api-6802e1aa4f78 16/24
8/15/23, 1:40 PM Repository Pattern and Unit of Work with ASP.NET Core Web API | by Yohan Malshika | Enlear Academy
190 4
Welcome back to my blog. Today we are going to learn Footer snippet by combining HTML,
CSS, Javascript and Bootstrap . Come on, let’s find…
109 1
143
https://enlear.academy/repository-pattern-and-unit-of-work-with-asp-net-core-web-api-6802e1aa4f78 18/24
8/15/23, 1:40 PM Repository Pattern and Unit of Work with ASP.NET Core Web API | by Yohan Malshika | Enlear Academy
119
https://enlear.academy/repository-pattern-and-unit-of-work-with-asp-net-core-web-api-6802e1aa4f78 19/24
8/15/23, 1:40 PM Repository Pattern and Unit of Work with ASP.NET Core Web API | by Yohan Malshika | Enlear Academy
Itzhak
Edin Šahbaz
https://enlear.academy/repository-pattern-and-unit-of-work-with-asp-net-core-web-api-6802e1aa4f78 20/24
8/15/23, 1:40 PM Repository Pattern and Unit of Work with ASP.NET Core Web API | by Yohan Malshika | Enlear Academy
10 1
Lists
Staff Picks
395 stories · 215 saves
Self-Improvement 101
20 stories · 403 saves
Productivity 101
20 stories · 410 saves
https://enlear.academy/repository-pattern-and-unit-of-work-with-asp-net-core-web-api-6802e1aa4f78 21/24
8/15/23, 1:40 PM Repository Pattern and Unit of Work with ASP.NET Core Web API | by Yohan Malshika | Enlear Academy
591 2
Belarmino Silva
114 1
https://enlear.academy/repository-pattern-and-unit-of-work-with-asp-net-core-web-api-6802e1aa4f78 22/24
8/15/23, 1:40 PM Repository Pattern and Unit of Work with ASP.NET Core Web API | by Yohan Malshika | Enlear Academy
Kamlesh Singh
15
https://enlear.academy/repository-pattern-and-unit-of-work-with-asp-net-core-web-api-6802e1aa4f78 23/24
8/15/23, 1:40 PM Repository Pattern and Unit of Work with ASP.NET Core Web API | by Yohan Malshika | Enlear Academy
29 2
https://enlear.academy/repository-pattern-and-unit-of-work-with-asp-net-core-web-api-6802e1aa4f78 24/24