如何解决这个通用的存储库模式问题?
我从上一个问题中得到了这段代码,但它没有编译:
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 class Repository : IRepository
2- public class CustomerRepository : Repository, ICustomerRepository
它给了我这个错误: 使用通用类型 'TestApplication1.IRepository '需要'2'类型参数
你能帮我解决吗?
I had this code from a previous question, but its not compiling:
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
}
But in these 2 lines :
1- public class Repository : IRepository
2- public class CustomerRepository : Repository, ICustomerRepository
It give me this error: Using the generic type 'TestApplication1.IRepository' requires '2' type arguments
can you help me solve?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
从 Repository/IRepository 继承时需要使用两个类型参数,因为它们采用两个类型参数。也就是说,当您从 IRepository 继承时,您需要指定类似:
和
Edited 以在 Repository 的实现上添加类型约束
You need to use two type arguments when inheriting from Repository/IRepository because they take take two type arguments. Namely, when you inherit from IRepository, you need to specify something like:
and
Edited to add type constraint on implementation of Reposistory
当您实现通用接口时,您还需要提供通用接口类型规范。将这两行更改为:
和
When you are implementing a generic interface, you need to provide the generic interface type specifications as well. Change those two lines to:
and