Overriding Existing Methods
As you can see in standard implementation, all methods are marked as virtual which means you can customize their content via overriding them.
You can override the existing methods to create a new class. To do that, newly created classes should inherit from RENUnitOfWork class:
public class MyUnitOfWork<TDbContext> : RENUnitOfWork<TDbContext> where TDbContext : RENDbContext
{
public MyUnitOfWork(TDbContext context) : base(context) { }
public override IRENRepository<TEntity>? GetRENRepository<TEntity>()
{
// Make custom actions here
return base.GetRENRepository<TEntity>();
}
// we didn't override SaveChanges() method because we don't want to change it's behaviour
}
Here, we overrided the existing method to expand it's functionality. From now on, we have register MyUnitOfWork in Program.cs instead of standard registeration:
// builder.Services.RegisterRENDatabaseAccessHelpers(); // SINCE WE ARE NOT USING STANDARD APPROACH ANYMORE
builder.Services.AddScoped(typeof(IRENUnitOfWork<>), typeof(MyUnitOfWork<>));
And we may inject it to our classes:
public class HomeController : ControllerBase
{
private readonly IRENUnitOfWork<RENDbContext> _uow;
private readonly IRENRepository<User> _customUserRepository;
public HomeController(IRENUnitOfWork<RENDbContext> uow)
{
_uow = uow;
_customUserRepository = uow.GetRENRepository<User>();
}
}
Here, you will use your MyUnitOfWork class thanks to registeration.
Last updated