C# Repository Pattern

Repository Pattern

The repository pattern provides the common interface to extracting the business entities. The developer is always looking for good project architecture, which reduces repetitive code and separates the Data Access Layer and Business Logic Layer.

Now, we create an Employee entity and the code snippet, mentioned below is for an Employee entity.


  1. using System;  
  2. using System.ComponentModel.DataAnnotations;  
  3. using System.ComponentModel.DataAnnotations.Schema;  
  4. namespace EntityDomain {  
  5.     public class Employee {  
  6.         [DatabaseGenerated(DatabaseGenerationOption.Identity)]  
  7.         [Key]  
  8.         public int Id {  
  9.             get;  
  10.             set;  
  11.         }  
  12.         public string LastName {  
  13.             get;  
  14.             set;  
  15.         }  
  16.         public string FirstName {  
  17.             get;  
  18.             set;  
  19.         }  
  20.         public DateTime BirthDate {  
  21.             get;  
  22.             set;  
  23.         }  
  24.     }  
  25. }  

Now, we create class MyContext, The code snippet, mentioned below is for MyContext.


  1. using System.Data.Entity;  
  2. namespace EntityDomain {  
  3.     public class MyContext: DbContext {  
  4.         public MyContext(): base("Dbconnection") {  
  5.             Database.SetInitializer < MyContext > (null);  
  6.         }  
  7.         public virtual DbSet < Employee > Employee {  
  8.             get;  
  9.             set;  
  10.         }  
  11.     }  
  12. }  

Now, create a repository interface for the entity operations, so that we develop loosely coupled Application.


  1. using Microsoft.EntityFramework;  
  2. using System;  
  3. using System.Collections.Generic;  
  4. using System.Linq;  
  5. namespace EntityDomain {  
  6.     public interface IRepository < T > where T: Class {  
  7.         IEnumerable < T > GetAll {  
  8.             get;  
  9.         }  
  10.         void Add(T entity);  
  11.         void Delete(T entity);  
  12.         void Update(T entity);  
  13.         T FindById(int Id);  
  14.     }  
  15. }  

Now, create a Repository class and implementIRepository interface. This repository has a parameterized constructor with a parameter as MyContext.


  1. using Microsoft.EntityFramework;  
  2. using System;  
  3. using System.Collections.Generic;  
  4. using System.Linq;  
  5. namespace EntityDomain {  
  6.     public class Repository: IRepository < T > where T: Class {  
  7.         private readonly MyContext context;  
  8.         private DbSet < T > DbSet;  
  9.         public Repository(MyContext _context) {  
  10.             this.context = _context;  
  11.             DbSet = context.Set < T > ();  
  12.         }  
  13.         public IEnumerable < T > GetAll() {  
  14.             return DbSet.AsEnumerable();  
  15.         }  
  16.         public void Add(T entity) {  
  17.             DbSet.Add(entity);  
  18.             context.SaveChanges();  
  19.         }  
  20.         public void Update(T entity) {  
  21.             DbSet.Attach(entity);  
  22.             context.Entry(entity).State = EntityState.Modified;  
  23.             context.SaveChanges();  
  24.         }  
  25.         public void Delete(T entity) {  
  26.             DbSet.Remove(entity);  
  27.             context.SaveChanges();  
  28.         }  
  29.         public T FindById(int id) {  
  30.             return DbSet.Find(id);  
  31.         }  
  32.     }  
  33. }  

NOTE

Here T is an entity like an Employee.

I hope, you will enjoyed the blog. Happy coding.