Articles → .NET DESIGN PATTERN → Repository Pattern In C#

Repository Pattern In C#






What Is A Repository Pattern?





Steps To Implement The Repository Pattern


  1. Identify the entities like employee, department, salary, etc
  2. Create a class of the entities
  3. Create an interface with all CRUD operations
  4. Implement the repository interface

Code


public interface IRepository <T> {
  void Add(T entity);
  void Update(T entity);
  void Delete(T entity);
  T GetById(int Id);
  IEnumerable <T> GetAll();
}

public class Employee {
  public int Id {
    get;
    set;
  }
  public string EmployeeName {
    get;
    set;
  }
}

public class EmployeeRepository: IRepository <Employee> {
  //  Declare DbContext
  public void Add(Employee entity) {
    //  Write a logic for adding an employee
  }

  public void Delete(Employee entity) {
    //  Write a logic for delete an employee
  }

  public IEnumerable <Employee> GetAll() {
    //  Write a logic to get the list of all employees
    throw new NotImplementedException();
  }

  public Employee GetById(int Id) {
    //  Write a logic to get the employee details from Id
    throw new NotImplementedException();
  }

  public void Update(Employee entity) {
    //  Write a logic for update an employee
    throw new NotImplementedException();
  }
}



Posted By  -  Karan Gupta
 
Posted On  -  Sunday, December 27, 2020

Query/Feedback


Your Email Id  
 
Subject 
 
Query/FeedbackCharacters remaining 250