我从上一个问题中得到了这段代码,但它没有编译:
public interface IEntity
{
// Common to all Data Objects
}
public interface ICustomer : IEntity
{
// Specific data for a customer
}
public interface IRepository<T, TID> : IDisposable where T : IEntity
{
T Get(TID key);
IList<T> GetAll();
void Save (T entity);
T Update (T entity);
// Common data will be added here
}
public class Repository<T, TID> : IRepository
{
// Implementation of the generic repository
}
public interface ICustomerRepository
{
// Specific operations for the customers repository
}
public class CustomerRepository : Repository<ICustomer>, ICustomerRepository
{
// Implementation of the specific customers repository
}
但是在这两行中:
1- 公共(public)类存储库:IRepository
2- 公共(public)类 CustomerRepository : Repository, ICustomerRepository
它给我这个错误:使用通用类型“TestApplication1.IRepository”需要“2”个类型参数
你能帮我解决吗?
最佳答案
从 Repository/IRepository 继承时需要使用两个类型参数,因为它们采用两个类型参数。也就是说,当您从 IRepository 继承时,您需要指定如下内容:
public class Repository<T, TID> : IRepository<T,TID> where T:IEntity
和
public class CustomerRepository : Repository<ICustomer,int>,ICustomerRepository
编辑以在 Reposistory 的实现上添加类型约束
关于c# - 如何解决这个通用的存储库模式问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1495574/