Using Both (Extend & Override)
You’re not limited to just one approach—you can combine overriding existing methods and adding new ones in a single custom repository class.
This gives you maximum flexibility:
Override methods to change or enhance built-in repository behavior.
Extend by introducing brand new methods tailored to your project’s unique requirements.
For example, you might override GetListAsync
to add caching or custom filtering logic, while also introducing a brand new method like FindByDepartmentId
.
This is the real power of REN.Kit’s repository design: your data layer can be as standard or as custom as you want.
Pro Tip:
Just inherit from the base repository (like RENRepository<TEntity>
), override any method you want, and add your own new methods—all in the same class!
How to Use Both? (Override + Extend)
1. Create a New Interface for Your Custom Methods
public interface IExtendedRENRepository<TEntity> : IRENRepository<TEntity>
where TEntity : class
{
void AdditionalMethod();
}
This interface introduces your new method(s) while keeping all defaults.
2. Implement Your Custom Repository
public class ExtendedAndOverridedRENRepository<TEntity>(DbContext context)
: RENRepository<TEntity>(context),
IExtendedRENRepository<TEntity> where TEntity : class
{
public void AdditionalMethod()
{
Console.WriteLine("ExtendedAndOverridedRENRepository AdditionalMethod called.");
// Implement your additional logic here
}
public override async Task<List<TEntity>> GetListAsync(
Expression<Func<TEntity, bool>>? filter = null,
Func<IQueryable<TEntity>, IOrderedQueryable<TEntity>>? orderBy = null,
Func<IQueryable<TEntity>, IQueryable<TEntity>>? include = null,
bool isReadOnly = false,
CancellationToken cancellationToken = default)
{
Console.WriteLine("ExtendedAndOverridedRENRepository GetListAsync called");
// Add custom logic here
return await base.GetListAsync(filter, orderBy, include, isReadOnly, cancellationToken);
}
}
3. Register Your Custom Implementation
Since you’re introducing a new interface and class, register both:
builder.Services.RegisterRENRepository<IExtendedRENRepository<Employee>, ExtendedAndOverridedRENRepository<Employee>>();
Reminder:
Always obtain repositories via Unit of Work! Do not inject repositories directly—call them through your UoW instance to ensure transactional consistency.
4. Use Your Custom Repository in Action
[Route("api/[controller]")]
[ApiController]
public class ExtendedAndOverridedRepositoryDataController(IExtendedRENUnitOfWork<RenDbContext> unitOfWork) : ControllerBase
{
private readonly IExtendedRENRepository<Employee> customEmployeeRepository =
unitOfWork.GetRepository<ExtendedAndOverridedRENRepository<Employee>, Employee>();
[HttpGet("additional-method")]
public IActionResult AdditionalMethod()
{
customEmployeeRepository.AdditionalMethod();
return Ok("Additional method called successfully.");
}
[HttpGet("get-employees")]
public async Task<IActionResult> GetEmployees()
{
var employees = await customEmployeeRepository.GetListAsync();
return Ok(employees);
}
}
Last updated