如何解决这个泛型与仓库模式问题? [英] How to solve this generic with repository pattern problem?

查看:196
本文介绍了如何解决这个泛型与仓库模式问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个前一个问题的代码,但不是编译:

  public interface IEntity 
{
//所有数据对象共有
}
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
{
//实现特定客户资料库
}
/ pre>

但是在这两行中:



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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆