简体   繁体   English

接口可以要求实现基类吗?

[英]Can an interface require a base class to be implemented?

I have an abstract BaseRepository which handles basic CRUD operations.我有一个抽象的 BaseRepository 来处理基本的 CRUD 操作。

public abstract class BaseRepository
{
    protected readonly IDbContextFactory<DbContext> _dbContextFactory;

    public BaseRepository(IDbContextFactory<TrinityDbContext> dbContextFactory)
    {
        _dbContextFactory = dbContextFactory;
    }
}

public abstract class BaseRepository<T> : BaseRepository where T : class, IUniqueIdentifier
{
    public BaseRepository(IDbContextFactory<TrinityDbContext> dbContextFactory) : base(dbContextFactory) { }
}

I created an abstract RepoServiceBase to add those CRUD operations to my services.我创建了一个抽象的 RepoServiceBase 来将这些 CRUD 操作添加到我的服务中。

public abstract class RepoServiceBase<T> where T : class, IUniqueIdentifier
{
    private readonly BaseRepository<T> _repo;

    public RepoServiceBase(BaseRepository<T> repo)
    {
        _repo = repo;
    }
}

But when constructing the service I get the following error: Cannot convert from IProductRepository to BaseRepository但是在构建服务时出现以下错误:无法从 IProductRepository 转换为 BaseRepository

public class ProductService : RepoServiceBase<Product>, IProductService
{
    public ProductService(IProductRepository repo) : base(repo) { }
}

Is there a way to require IProductRepository to implement BaseRepository?有没有办法要求 IProductRepository 实现 BaseRepository?

Is there a way to require IProductService to implement BaseRepository?有没有办法要求 IProductService 实现 BaseRepository?

No, you can't do this.不,你不能这样做。

But you can create additional interfaces to handle that.但是您可以创建额外的接口来处理它。

Create interfaces for base repositores:为基础存储库创建接口:

public interface IBaseRepository
{
    // methods
}

public interface IBaseRepository<T> : IBaseRepository
{
    // methods
}

Implement these interfaces by base repositories:通过基本存储库实现这些接口:

public abstract class BaseRepository : IBaseRepository
{
    protected readonly IDbContextFactory<DbContext> _dbContextFactory;

    public BaseRepository(IDbContextFactory<TrinityDbContext> dbContextFactory)
    {
        _dbContextFactory = dbContextFactory;
    }
}

public abstract class BaseRepository<T> : BaseRepository, IBaseRepository<T> where T : class, IUniqueIdentifier
{
    public BaseRepository(IDbContextFactory<TrinityDbContext> dbContextFactory) : base(dbContextFactory) { }
}

Now your product repository stuff should look like this:现在您的产品存储库应该如下所示:

public interface IProductRepository : IBaseRepository<Product>
{
    // methods
}

public class ProductRepository : BaseRepository<Product>, IProductRepository
{
    // implementation
}

Change RepoServiceBase :更改RepoServiceBase

public abstract class RepoServiceBase<T> where T : class, IUniqueIdentifier
{
    private readonly IBaseRepository<T> _repo;

    public RepoServiceBase(IBaseRepository<T> repo)
    {
        _repo = repo;
    }
}

You can ignore IBaseRepository interface (non-generic one) as you expect only generic version in constructor params.您可以忽略IBaseRepository接口(非通用接口),因为您期望构造函数参数中只有通用版本。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么我不能将基类引用转换为派生类实现的变量接口? - Why can't I cast base reference to a variant interface implemented by derived class? 如何使用抽象类需要后代类实现接口? - How to have an abstract class require an interface to be implemented by descendant classes? 通过由基类c#实现的接口调用类的方法 - Calling a method of a class through the interface implemented by the base class c# 如何在基类上调用显式实现的接口方法 - How to call an explicitly implemented interface-method on the base class C#-需要基类上的接口,但仅派生类中的实现 - C# - require an interface on a base class but only the implementations in derived classes 属性可以要求继承或实现类或接口吗? - Can an attribute require inheritance or implementation of a class or interface? 在Base类中声明的C#接口方法不需要在Derived类中再次实现 - C# interface method declared in Base class need not be again implemented in Derived class 防止在派生类C#中调用基类实现的接口方法 - Prevent calling base class implemented interface method in the derived class C# 我是否需要在派生类的定义中指定由基类实现的接口? - Do I need to specify interface implemented by a base class in the definition of a derived class? 默认实现的接口方法可以在class中隐式实现吗? - Can the interface method with default implementation be implemented implicitly in a class?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM