Using REN.Kit's Built-in Repository Pattern
How to Use the Built-In Repository
[Route("api/[controller]")]
[ApiController]
public class EmployeesController(IRENUnitOfWork<AppDbContext> unitOfWork) : ControllerBase
{
/// <summary>
/// Retrieves all employees.
/// </summary>
[HttpGet]
public async Task<IActionResult> GetAllEmployees()
{
// you can also create a variable and assign unitOfWork.GetRepository<Employee>()
var employees = await unitOfWork.GetRepository<Employee>().GetListAsync();
return Ok(employees);
}
/// <summary>
/// Finds a single employee by ID.
/// </summary>
[HttpGet("{id:guid}")]
public async Task<IActionResult> GetEmployee(Guid id)
{
var employeeRepository = unitOfWork.GetRepository<Employee>();
var employee = await employeeRepository.GetSingleAsync(e => e.Id == id);
return employee is not null ? Ok(employee) : NotFound();
}
}Supported Repository Operations
Last updated