Implementing Additional Methods

Surely you should be able to implement new functions addition to existing one if you need it. This is SOLID after all! Let's see how we can do this. First you need to create the interface that inherits from IRENRepository interface. Your new interface should contain additional methods:

public interface IMyRepository<TEntity> : IRENRepository<TEntity> where TEntity : class
{
    Task MyCustomFunction(CancellationToken cancellationToken = default);
}

Then create your MyRepository class that inherits from IMyRepository:

public class MyRepository<TEntity> : RENRepository<TEntity>, IMyRepository<TEntity> where TEntity : class
{
    public MyRepository(RENDbContext context) : base(context) { }

    public Task MyCustomFunction(CancellationToken cancellationToken = default)
    {
        return Task.Factory.StartNew(() =>
        {
            cancellationToken.ThrowIfCancellationRequested();
            Console.WriteLine("This is my custom Function");
            // other custom implementations!

        }, cancellationToken);
    }
}
circle-info

Since we created new interface and want to use it, we can not override the GetRENRepository function because we want a function that returns our newly created interface.

Customize our UnitOfWork class to return our newly created interface:

Also update the interface:

Register your custom UnitOfWork class in your Program.cs:

And you can get and use your custom repository like this:

Last updated