Generic Repository Pattern In ASP.NET Core MVC And Entity Framework Core


This article introduces the CRUD operations using generic repository pattern in ASP.NET Core MVC and Entity Framework Core. We create two projects one is a web application for UI and another project is a class library for entities. We create the database from these entities using Entity Framework Core migrations. To keep this article simple and short, we use two entities named Customer and Address to perform CRUD operations. These entities have one to many relationship.

Why Generic Repository Pattern


We perform common operations such as insert, update, delete, read, paging, sorting and search etc on a database table. We have to create methods for these operations based on each database table. Hence, We have to create approx seven methods for each entity. Similarly, we have to create 70 methods for 10 entities.

In short, we can say that we are repeating the code for each entity’s to perform basic operations which are against the DRY principle in the software engineering; that’s why we need a generic repository pattern.

It reduces to development time and easy to maintain the application.

Create Data Model


We create entities in a separate class library project named GRP.Data. It uses the Code First approach of Entity Framework Core and SQL Server as a database. We have to install two NuGet packages into this project which are :

  • Microsoft.EntityFrameworkCore
  • Microsoft.EntityFrameworkCore.SqlServer

We perform the CRUD operations on entities. We create three entities which are BaseEntity, Customer and Address. TheBaseEntity class has common properties for each entity that’s why it will be inherited by rest of entities. Customer and Address entities have one to many relationship. A customer can have more than one address.

Figure 1: One to many relationship between Customer and Address tables

We create a class for the BaseEntity. The following code snippet for same.

Now, we create a class for Customer entity which will be used to create database table as well. It inherits from the BaseEntity class as per following code snippet.

We define the configuration to create database table from Customer entity. The following is a code snippet for the mapping of the Customer entity (CustomerMap.cs).

Now, we create an Address entity, which inherits from the BaseEntity class. The following is code snippet for the Address entity.

We define the configuration to create database table from Address entity. The following is a code snippet for the mapping of the Address entity (AddressMap.cs).

The GRP.Data project also contains DbContext. We create a context class ApplicationContext (ApplicationContext.cs) class. It inherits from DbContext class as per following code snippet.

We defined all entities for data model and context. There are two operations remaining to order to create a database from these entities. These operations are “Database Connectivity using EF” and “Database Initialize using EF Migration”. We can know about these operations with help of another article that is Repository Pattern In ASP.NET Core MVC And Entity Framework Core.

Implement Generic Repository Pattern


We create a single repository for both entities to perform CRUD operations. We create a generic interface IRepository to develop a loosely coupled application. The following code snippet for it in GRP.Data project.

Now, let’s implement the preceding interface on a class named Repository. This class has definitions of all methods to perform CRUD operations on the entity. It has a parameterized constructor that accepts Context as a parameter. It passes at a time of repository instance created by dependency injection using inbuilt IoC. The following is a code snippet for the Repository class under GRP.Data project.

Now, we created both repository and database. Web application communicates with the repository. It is a data access layer. We register repository to the dependency injection during the application startup. We update the ConfigureServices method in the Startup class. The following code snippet for same.

Create Application User Interface


Now, we create a web application to perform CRUD operation. We create a controller named CustomerController under the Controllers folder of the application.

This controller has action methods for each user interface to perform the CRUD operations. We create an IRepository interface instance then inject it in the controller’s constructor to get an object of Repository. The following is a code snippet for the CustomerController.

Now, let’s develop the user interface to perform the CRUD operations on Customer and Address entities. We develop views for adding and editing a customer, listing, and deletion.

Customer Listing

We create a view model named CustomerListingViewModel that binds to UI for the customer listing. The following code snippet for same.

We create a view to show all customers in the listing. It displays customer data in a tabular format including adding a new customer option. The customer listing has options to edit and delete a customer individually. It is an index view named index.cshtml under Customer folder of Views. The following code snippet for same.

Let’s run the application. It makes a GET request for index() action method and as a response all the customers listed in the UI as in Figure 2.

Figure 2: Customer Listing

Create / Edit Customer View

We create a view model named CustomerViewModel that uses to performs add and edit operation. The following code snippet for same.

As create and edit views have same input fields, So we create a common partial view for both named _AddEditCustomer. The view uses the preceding view model. The following is the code snippet for _AddEditCustomer.cshtml.

On the landing page, click on either ‘Add Customer’ or ‘Edit’ buttons in the customer listing. It makes a GET request for AddEditCustomer action method which sends partial view as a response. The following UI shows form for add/edit a customer.

Figure 3: Add/Edit a Customer

Delete a Customer

Now, Delete operation performs against each customer. The customer has a delete button in the listing. When a user clicks on delete button against a customer, a confirmation popup shows to ask “Are you want to delete xxx?. This pop-up has two buttons Delete and Cancel. When a user clicks on Delete button of pop-up then it makes an HttpPost request that calls the DeleteCustomer action method and deletes the customer. The following is code snippet for _DeleteCustomer.cshtml

Let’s click on Delete button in the customer listing that shows a confirmation popup as in Figure 4 to delete a customer.

Figure 4: Delete a customer

Conclusion


This article demonstrated CRUD operations using Generic Repository Pattern in ASP.NET Core MVC with Entity Framework Core Code First approach. The complete source code is available at GitHub, click here to download.