Repository and Unit of Work Patterns in an ASP.NET MVC Application

Implementing the Repository and Unit of Work Patterns in an ASP.NET MVC Application

The repository and unit of work patterns are intended to create an abstraction layer between the data access layer and the business logic layer of an application. Implementing these patterns can help insulate your application from changes in the data store and can facilitate automated unit testing or test-driven development (TDD).

In this tutorial you'll implement a repository class for each entity type. For the Employee entity type you'll create a repository interface and a repository class. When you instantiate the repository in your controller, you'll use the interface so that the controller will accept a reference to any object that implements the repository interface. When the controller runs under a web server, it receives a repository that works with the Entity Framework. When the controller runs under a unit test class, it receives a repository that works with data stored in a way that you can easily manipulate for testing, such as an in-memory collection.
The following illustration shows one way to conceptualize the relationships between the controller and context classes compared to not using the repository or unit of work pattern at all.






Creating the EmployeeRepository Repository Class


In the Interface folder, create a class file named IEmployeesRepository.cs and replace the existing code with the following code:





In the Repository folder, create a class file named EmployeeRepository.cs file. Replace the existing code with the following code, which implements the IEmployeesRepository interface:






Employee Controller to Use the Repository

In EmployeeController.cs, replace the code currently in the class with the following code. The changes are highlighted.

using Repository.Interface;
using Repository.Repository;
using Repository.ViewModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace TestApps.Controllers
{
    public class EmployeeController : Controller
    {
        // GET: Employee
        private IEmployeesRepository employeesRepository;
        public EmployeeController(): this (new EmployeeRepository())
        {

        }
        public EmployeeController(IEmployeesRepository employeesRepository)
        {
            this.employeesRepository = employeesRepository;
        }

        public ActionResult Index()
        {

            return View();
        }

        public object GetEmployees()
        {
            IEnumerable<EmployeeViewModel> data = employeesRepository.GetEmployees();

            return Json(data, JsonRequestBehavior.AllowGet);
        }

     
    }
}
Run the site and Employee/GetEmployees


Benefits of Repository Pattern
  1. It centralizes data logic or business logic and service logic.
  2. It gives a substitution point for the unit tests.
  3. Provides a flexible architecture.
  4. If you want to modify the data access logic or business access logic, you don’t need to change the repository logic.


Comments