Articles → .NET DESIGN PATTERN → Repository Pattern In C#
Repository Pattern In C#
What Is A Repository Pattern?
- Identify the entities like employee, department, salary etc.
- Create a class of the entities.
- Create an interface with all the basic operations i.e. Add, Update, Delete, GetById, GetAll.
- Implement the repository interface.
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();
}
}