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

发布于 2025-02-06 23:19:30 字数 1128 浏览 5 评论 0原文

我有一个抽象的持久性,可以处理基本的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) { }
}

我创建了一个抽象的存储库,以将这些CRUD操作添加到我的服务中。

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

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

但是在构建服务时,我会收到以下错误: 无法从iProductrepository转换为Baseerepository,

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

是否有一种方法要求Iproductrepository实现Baseerepository?

I have an abstract BaseRepository which handles basic CRUD operations.

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.

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

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

Is there a way to require IProductRepository to implement BaseRepository?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

荒人说梦 2025-02-13 23:19:30

是否有一种方法要求IproductService实现BaseerePository?

不,你不能这样做。

但是您可以创建其他接口来处理它。

创建基本存储库的接口:

public interface IBaseRepository
{
    // methods
}

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

通过基础存储库来实现这些接口:

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) { }
}

现在您的产品存储库应该看起来像这样:

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

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

更改repoServiceBase

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

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

您可以忽略ibaseRepository接口)正如您所期望的,仅在构造函数参数中使用通用版本。

Is there a way to require IProductService to implement 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:

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文