如何用存储库模式问题解决这个generics?

我从上一个问题得到了这个代码,但它没有编译:

public interface IEntity { // Common to all Data Objects } public interface ICustomer : IEntity { // Specific data for a customer } public interface IRepository : IDisposable where T : IEntity { T Get(TID key); IList GetAll(); void Save (T entity); T Update (T entity); // Common data will be added here } public class Repository : IRepository { // Implementation of the generic repository } public interface ICustomerRepository { // Specific operations for the customers repository } public class CustomerRepository : Repository, ICustomerRepository { // Implementation of the specific customers repository } 

但在这两行中:

1-公共类存储库:IRepository

2-公共类CustomerRepository:Repository,ICustomerRepository

它给了我这个错误:使用generics类型’TestApplication1.IRepository’需要’2’类型参数

你能帮我解决一下吗?

从Repository / IRepositoryinheritance时需要使用两个类型参数,因为它们需要两个类型参数。 也就是说,当您从IRepositoryinheritance时,您需要指定以下内容:

 public class Repository : IRepository where T:IEntity 

 public class CustomerRepository : Repository,ICustomerRepository 

编辑为Reposistory的实现添加类型约束

在实现通用接口时,还需要提供通用接口类型规范。 将这两行更改为:

 public class Repository : IRepository where T : IEntity { // ... 

 public class CustomerRepository : Repository, ICustomerRepository { // ...