Articles → .NET DESIGN PATTERN → Repository Pattern In C#
Repository Pattern In C#
What Is A Repository Pattern?
Steps To Implement The Repository Pattern
- Identify entities such as employees, departments, salaries, and others.
- Create a class of entities.
- Design an interface that includes all CRUD operations.
- 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 |