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.
- 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 in 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().ToList();
- }
- public T SelectById(int id)
- {
- return db.Set().Find(id);
- }
- public void Insert(T obj)
- {
- db.Set().Add(obj);
- }
- public void Update(T obj)
- {
- db.Entry(obj).State = EntityState.Modified;
- }
- public void Delete(int id)
- {
- T obj = db.Set().Find(Id);
- this.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 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.