Implementing New Methods

Sometimes your project needs data access operations or utilities that go beyond what the standard Repository provides. Thanks to REN.Kit’s extensible architecture, you can easily add brand new methods to your custom Repository—without altering the base implementation!


How to Extend?

1. Create a New Service Interface to Define Your Method

Define an interface that extends the default Repository interface and adds your new method(s):

 public interface IExtendedRENRepository<TEntity> 
      : IRENRepository<TEntity> where TEntity : class
 {
     void AdditionalMethod();
 }

This interface introduces your new method(s) while keeping all the standard Repository features.


2. Create a New Repository Class Implementing the Interface

Implement your interface by inheriting from RENRepository<TEntity> and adding your custom logic:

public class ExtendedRENRepository<TEntity>(RenDbContext dbContext) 
    : RENRepository<TEntity>(dbContext), 
    IExtendedRENRepository<TEntity> where TEntity : class
{
    public void AdditionalMethod()
    {
        Console.WriteLine("ExtendedRENRepository AdditionalMethod called.");
        // Implement your additional logic here
    }
}

3. Register Your Custom Repository

Although you define and register your custom repository and its interface, the recommended approach in REN.Kit is not to inject repositories directly. Instead, always access your repositories through the Unit of Work instance.

This ensures all your data operations—queries, updates, transactions—are tracked, coordinated, and committed consistently.

When requesting a repository from your Unit of Work, specify your custom repository type using the generic overload:

private readonly IExtendedRENRepository<Employee> customEmployeeRepository = unitOfWork.GetRepository<ExtendedRENRepository<Employee>, Employee>();

This approach ensures that your custom implementation is used for all data access operations involving the specified entity type.

IMPORTANT!


4. Use Your Custom Repository in Action

[Route("api/[controller]")]
[ApiController]
public class ExtendedRepositoryDataController(IRENUnitOfWork<RenDbContext> unitOfWork) : ControllerBase
{
    private readonly IExtendedRENRepository<Employee> customEmployeeRepository = unitOfWork.GetRepository<ExtendedRENRepository<Employee>, Employee>();

    [HttpGet("find")]
    public IActionResult GetRepository(Guid key)
    {
        var employee = customEmployeeRepository.AdditionalMethod();
        return Ok();
    }
}

Pro Tip:

Last updated