Amr*_*rhy 4 c# generics design-patterns repository-pattern
我从前一个问题获得了此代码,但它没有编译:
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
}
Run Code Online (Sandbox Code Playgroud)
但是在这两行中:
1-公共类存储库:IRepository
2-公共类CustomerRepository:Repository,ICustomerRepository
它给了我这个错误:使用泛型类型'TestApplication1.IRepository'需要'2'类型参数
你能帮我解决一下吗?
从Repository/IRepository继承时,需要使用两个类型参数,因为它们采用两个类型参数.也就是说,当您从IRepository继承时,您需要指定以下内容:
public class Repository<T, TID> : IRepository<T,TID> where T:IEntity
Run Code Online (Sandbox Code Playgroud)
和
public class CustomerRepository : Repository<ICustomer,int>,ICustomerRepository
Run Code Online (Sandbox Code Playgroud)
编辑为Reposistory的实现添加类型约束