Base solution for your next web application
Open Closed

Passing a custom Repository to a ABP base class #12251


User avatar
0
Astech created

We have a custom repository that we would like to use everywhere. However, when we try to change a repository that is being passed to a base class we are having issues. Please see below as an example in LoginManager:

public class LogInManager : AbpLogInManager<Tenant, Role, User>
{
    public LogInManager(IOurCustomRepository<Tenant> tenantRepository) 
                                        : base(tenantRepository)
    {
        ...
    }
}

This results in a compile time error of

Argument 3: cannot convert from 'OurProject.Repositories.IOurCustomRepository<OurProject.MultiTenancy.Tenant>' to 'Abp.Domain.Repositories.IRepository<OurProject.MultiTenancy.Tenant>'

We have also tried casting the repository when passed to base:

public class LogInManager : AbpLogInManager<Tenant, Role, User>
{
    public LogInManager(IOurCustomRepository<Tenant> tenantRepository) 
                                        : base((IRepository<Tenant>)tenantRepository)
    {
        ...
    }
}

This allows the code to build but results in the following runtime error:

ComponentActivator: could not instantiate OurProject.Authorization.LogInManager
 ---> System.InvalidCastException: Unable to cast object of type 'OurProject.EntityFrameworkCore.Repositories.OurCustomRepository`1[OurProject.MultiTenancy.Tenant]' to type 'Abp.Domain.Repositories.IRepository`1[OurProject.MultiTenancy.Tenant]'.

How can we use our custom repository in this way while still satifying the base? I am aware that we can change the registration of IRepository so that a different implementation is used via Dependency Injection however we wish to specify our own custom repository in this way.

It's important to add that our custom repository does inherit from IRepository:

public interface IOurCustomRepository<TEntity, TPrimaryKey> : IRepository<TEntity, TPrimaryKey> where TEntity : class, IEntity<TPrimaryKey>

This is why we believe this should be possible?

Thank you


1 Answer(s)
  • User Avatar
    0
    oguzhanagir created
    Support Team

    Hi

    If you want to pass your CustomRepository to the AbpLoginManager class here, it should have a signature structure as specified below. It should be a class that inherits this interface class. By using ITransientDependency, we will indicate that we will use it temporarily for dependency injection.

    public interface IOurCustomRepository : IRepository<Tenant>, IRepository, ITransientDependency
    {
    }