In this article, we will learn about Repository pattern which is mostly used to create enterprise applications. Repository pattern divides 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 repository pattern. We can create any type of project structure to implement repository pattern. For example, we can create a repository class in MVC project folder itself or we can create different class library project in same solution or we can use onion architecture to implement it that contains different class library projects and MVC project in a solution. For simplicity, we will create a Repository class library project and one MVC project in a solution. In Repository pattern, we can use generic repository to implement CRUD operations that can be used by any entity of project and custom repository for implementing specific operations of particular entity. First create a Client class in MVC project.

  1. namespace Client.Web.Models  
  2. {  
  3.     class Client  
  4.     {  
  5.         public int Id { get; set; }  
  6.         public string Name { get; set; }  
  7.         public string City { get; set; }  
  8.         public string Email { get; set; }  
  9.         public string Mobile { get; set; }  
  10.     }  
  11. }  

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

  1. namespace Client.Repository  
  2. {  
  3.     public interface IRepository<T> where T:class  
  4.     {  
  5.         IEnumerable<T> SelectAll();  
  6.         T SelectById(int id);  
  7.         void Insert(T obj);  
  8.         void Update(T obj);  
  9.         void Delete(int id);  
  10.         void Save();  
  11.     }  
  12. }  

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

  1. namespace Client.Repository  
  2. {  
  3.     public class Repository<T>:IRepository<T> where T:class  
  4.     {  
  5.         protected readonly DbContext db;  
  6.         public Repository(DbContext _db)  
  7.         {  
  8.             db = _db;  
  9.         }  
  10.         public IEnumerable<T> SelectAll()  
  11.         {  
  12.             return db.Set().ToList();  
  13.         }  
  14.         public T SelectById(int id)  
  15.         {  
  16.             return db.Set().Find(id);  
  17.         }  
  18.         public void Insert(T obj)  
  19.         {  
  20.             db.Set().Add(obj);  
  21.         }  
  22.         public void Update(T obj)  
  23.         {  
  24.             db.Entry(obj).State = EntityState.Modified;  
  25.         }  
  26.         public void Delete(int id)  
  27.         {  
  28.              T obj = db.Set().Find(Id);  
  29.              this.Remove(obj);  
  30.         }  
  31.         public void Save()  
  32.         {  
  33.             db.SaveChanges();  
  34.         }  
  35.     }  
  36. }  

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:

  1. namespace Client.Web.Models  
  2. {  
  3.     using System;  
  4.     using System.Data.Entity;  
  5.     using System.Linq;  
  6.   
  7.     public class ClientContext : DbContext  
  8.     {  
  9.           
  10.         public ClientContext()  
  11.             : base("name=ClientContext")  
  12.         {  
  13.         }  
  14.           
  15.         public virtual DbSet<Client> Client  { get; set; }  
  16.     }  
  17. }  

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

Next Recommended Readings