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 Unit of Work class.
This gives you maximum flexibility:
Override methods to change or enhance built-in data/transaction behavior.
Extend by introducing brand new methods tailored to your project’s needs.
For example, you might override SaveChangesAsync
to add auditing, while also introducing a new utility method like ClearAllTables
.
This is the true power of REN.Kit’s design: your Unit of Work can be as standard or as custom as you need.
Pro Tip:
Just inherit from the base unit of work (like RENUnitOfWork<TDbContext>
), override any built-in methods 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 IExtendedRENUnitOfWork<TDbContext> : IRENUnitOfWork<TDbContext>
where TDbContext : DbContext
{
void AdditionalMethod();
}
This interface introduces your new method(s) while keeping all defaults.
2. Implement Your Custom Unit of Work
public class ExtendedAndOverridedRENUnitOfWork<TDbContext>(TDbContext context) : RENUnitOfWork<TDbContext>(context), IExtendedRENUnitOfWork<TDbContext> where TDbContext : DbContext
{
public void AdditionalMethod()
{
Console.WriteLine("ExtendedAndOverridedRENUnitOfWork AdditionalMethod called.");
// Implement your additional logic here
}
// Override existing method
public override IRENRepository<TEntity>? GetRepository<TEntity>()
{
Console.WriteLine("ExtendedAndOverridedRENUnitOfWork GetRepository called");
// You can add custom logic here before calling the base method
return base.GetRepository<TEntity>();
}
}
3. Register Your Custom Implementation
Since you’re introducing a new interface and class, register both:
builder.Services.RegisterRENDataServices<IExtendedRENUnitOfWork<RenDbContext>, ExtendedAndOverridedRENUnitOfWork<RenDbContext>>();
The registration method (for reference):
public static IServiceCollection RegisterRENDataServices<TIRENUnitOfWork, TRENUnitOfWork>(this IServiceCollection services)
{
services.AddScoped(typeof(TIRENUnitOfWork), typeof(TRENUnitOfWork));
return services;
}
4. Use Your Custom Unit of Work in Action
[Route("api/[controller]")]
[ApiController]
public class ExtendedAndOverridedDataController(IExtendedRENUnitOfWork<RenDbContext> unitOfWork) : ControllerBase
{
[HttpGet("additional-method")]
public IActionResult AdditionalMethod()
{
unitOfWork.AdditionalMethod();
return Ok("Additional method called successfully.");
}
[HttpGet("get-repository")]
public IActionResult GetRepository(string key)
{
unitOfWork.GetRepository<Employee>();
return Ok();
}
}
Pro Tip:
You can mix and match: override built-in transaction or repository logic, and add brand new helpers or project-specific utilities—all in one place.
And you're good to go!
With this approach, your Unit of Work becomes a tailored orchestration layer perfectly suited to your app’s evolving requirements.
Last updated