如何解决这个泛型与仓库模式问题? [英] How to solve this generic with repository pattern problem?
问题描述
我有一个前一个问题的代码,但不是编译:
public interface IEntity
/ pre>
{
//所有数据对象共有
}
public interface ICustomer:IEntity
{
//客户的具体数据
}
public接口IRepository< T,TID> :IDisposable where T:IEntity
{
T Get(TID key);
IList< T>得到所有();
void Save(T entity);
T更新(T实体);
//将在此添加常用数据
}
public class Repository< T,TID> :IRepository
{
//通用存储库的实现
}
public interface ICustomerRepository
{
//客户存储库的特定操作
}
public class CustomerRepository:Repository< ICustomer> ICustomerRepository
{
//实现特定客户资料库
}
但是在这两行中:
1- public class Repository:IRepository
2- public class CustomerRepository:Repository,ICustomerRepository
它给我这个错误:使用通用类型'TestApplication1.IRepository'需要'2'类型的参数
你可以帮我解决吗?
解决方案从Repository / IRepository继承时,需要使用两个类型的参数,因为它们需要两个类型的参数。也就是说,当您从IRepository继承时,您需要指定以下内容:
public class Repository< T,TID> :存储库< T,TID>其中T:IEntity
和
public class CustomerRepository:Repository< ICustomer,int>,ICustomerRepository
在Reposistory
的实现中添加类型约束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?
解决方案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:
public class Repository<T, TID> : IRepository<T,TID> where T:IEntity
and
public class CustomerRepository : Repository<ICustomer,int>,ICustomerRepository
Edited to add type constraint on implementation of Reposistory
这篇关于如何解决这个泛型与仓库模式问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!