Repository Pattern In ASP.NET MVC

In this article, we will learn about the Repository pattern which is mostly used to create enterprise applications. The repository pattern divides the application’s UI, business logic, and data access components into different layers that are easily maintainable and testable.

We will create a Client application to better understand the repository pattern. We can create any type of project structure to implement a repository pattern. For example, we can create a repository class in the MVC project folder itself or we can create different class library projects in the same solution or we can use onion architecture to implement it that contains different class library projects and MVC projects in a solution. For simplicity, we will create a Repository class library project and one MVC project in a solution. In the Repository pattern, we can use a generic repository to implement CRUD operations that can be used by any entity of the project and a custom repository for implementing specific operations of a particular entity. First, create a Client class in the MVC project.

namespace Client.Web.Models
{
    class Client
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public string City { get; set; }
        public string Email { get; set; }
        public string Mobile { get; set; }
    }
}

Next, we will create an interface for the Repository class library project. This interface contains generic database operations for any domain entity. We will name it IRepository.

namespace Client.Repository
{
    public interface IRepository<T> where T : class
    {
        IEnumerable<T> SelectAll();
        T SelectById(int id);
        void Insert(T obj);
        void Update(T obj);
        void Delete(int id);
        void Save();
    }
}

Next, we will create a repository class to implement this interface. We will name this class Repository.

namespace Client.Repository
{
    public class Repository<T> : IRepository<T> where T : class
    {
        protected readonly DbContext db;

        public Repository(DbContext _db)
        {
            db = _db;
        }

        public IEnumerable<T> SelectAll()
        {
            return db.Set<T>().ToList();
        }

        public T SelectById(int id)
        {
            return db.Set<T>().Find(id);
        }

        public void Insert(T obj)
        {
            db.Set<T>().Add(obj);
        }

        public void Update(T obj)
        {
            db.Entry(obj).State = EntityState.Modified;
        }

        public void Delete(int id)
        {
            T obj = db.Set<T>().Find(id);
            db.Set<T>().Remove(obj);
        }

        public void Save()
        {
            db.SaveChanges();
        }
    }
}

We are using Entity framework code first approach for data access so we will create a DbContext class.

The ClientContext class can be created as shown in the listing below.

namespace Client.Web.Models
{
    using System;
    using System.Data.Entity;
    using System.Linq;

    public class ClientContext : DbContext
    {
        public ClientContext()
            : base("name=ClientContext")
        {
        }

        public virtual DbSet<Client> Client { get; set; }
    }
}

Now we need to reference the Repository class library project in the MVC Web project. For this, Right click Client. Web project and add reference Client. Repository project. Now we will scaffold a controller in the Client. Web project. To scaffold, right-click the controller folder select a new controller with views using entity framework, and name it ClientController. To use the IRepository class for database operations, we will create an object of the Repository class and call methods of the Repository class. Now build and run the application. We should be able to perform CRUD operations.


Similar Articles