接口可以要求实现基类吗?
我有一个抽象的持久性,可以处理基本的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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不,你不能这样做。
但是您可以创建其他接口来处理它。
创建基本存储库的接口:
通过基础存储库来实现这些接口:
现在您的产品存储库应该看起来像这样:
更改
repoServiceBase
:您可以忽略
ibaseRepository
接口)正如您所期望的,仅在构造函数参数中使用通用版本。No, you can't do this.
But you can create additional interfaces to handle that.
Create interfaces for base repositores:
Implement these interfaces by base repositories:
Now your product repository stuff should look like this:
Change
RepoServiceBase
:You can ignore
IBaseRepository
interface (non-generic one) as you expect only generic version in constructor params.