Using REN.Kit's Built-in Repository Pattern
The easiest way to interact with your database entities is through REN.Kit’s built-in, generic repository—RENRepository. Forget about repetitive boilerplate or hand-crafted data access logic. Just call, query, and persist, using a reliable, production-ready pattern.
How to Use the Built-In Repository
Once your REN.Kit DataKit and Unit of Work are configured (see the previous setup), you never have to inject or instantiate repositories yourself. Always obtain your repository from the Unit of Work for full transaction support, context management, and best practices.
[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
With the RENRepository you can:
Query entities using full LINQ support, including filtering, sorting, and eager loading (
Include
).Retrieve single entities or collections (synchronously or asynchronously).
Get existence, count of entities with given parameters.
Insert, update, or delete entities—one by one or in bulk.
Use advanced operations like bulk insert/update/delete if needed.
Remain context-safe and transaction-friendly (because it’s always scoped to the Unit of Work).
Best Practice:
Always acquire your repository through the Unit of Work.
This ensures you’re using the right DbContext
instance and all your data changes are tracked, committed, or rolled back as a unit—guaranteeing transactional integrity.
Last updated