C# 存储库模式的通用接口继承和类实现

C# 存储库模式的通用接口继承和类实现,c#,generics,inheritance,c#-4.0,repository,C#,Generics,Inheritance,C# 4.0,Repository,我已经阅读了一些关于约束的内容,并试图在我的存储库模式中实现它 我想要下面这样的东西,但不能完全编译 public interface IRepository<T> { void GetAllData<T>(); } //This needs to inherit from IRepository //T has to be a model class //V has to be a class that implements IEmployeeRe

我已经阅读了一些关于约束的内容,并试图在我的存储库模式中实现它

我想要下面这样的东西,但不能完全编译

 public interface IRepository<T>
 {
    void GetAllData<T>();
 }

 //This needs to inherit from IRepository
 //T has to be a model class
 //V has to be a class that implements IEmployeeRepo
 public interface IEmployeeRepo<T, V> where V : EmployeeRepo where T : class : IRepository<T>
 {
    void DoSomethingEmployeeRelated();
 }

 //Dont think this inheritance is correct
 public class EmployeeRepo<Employee, this> : IEmployeeRepo
 {


 }

 //My example model class
 public class Employee
 {
     public string Name {get;set;}
 }
公共接口IRepository
{
void GetAllData();
}
//这需要从IRepository继承
//T必须是一个模范班
//V必须是一个实现IEmployeeRepo的类
公共接口IEEmployeeRepo其中V:EmployeeRepo其中T:class:IRepository
{
void DoSomethingEmployeeRelated();
}
//我不认为这种继承是正确的
公共类EmployeeRepo:IEEmployeeRepo
{
}
//我的示例模型类
公营雇员
{
公共字符串名称{get;set;}
}
试试这个

public interface IRepository<T>
 {
    void GetAllData<T>();
 }

 //This needs to inherit from IRepository
 //T has to be a model class
 //V has to be a class that implements IEmployeeRepo
 public interface IEmployeeRepo<T, V> : IRepository<T> where V : EmployeeRepo where T : class
 {
    void DoSomethingEmployeeRelated();
 }

 //Dont think this inheritance is correct
 public class EmployeeRepo : IEmployeeRepo<Employee, EmployeeRepo>
 {
    public void DoSomethingEmployeeRelated()
    {

    }

    public void GetAllData<Employee>()
    {

    }
 }

 //My example model class
 public class Employee
 {
     public string Name {get;set;}
 }
公共接口IRepository
{
void GetAllData();
}
//这需要从IRepository继承
//T必须是一个模范班
//V必须是一个实现IEmployeeRepo的类
公共接口IEEmployeeRepo:IRepository其中V:EmployeeRepo其中T:class
{
void DoSomethingEmployeeRelated();
}
//我不认为这种继承是正确的
公共类EmployeeRepo:IEEmployeeRepo
{
公共无效DoSomethingEmployeeRelated()
{
}
public void GetAllData()
{
}
}
//我的示例模型类
公营雇员
{
公共字符串名称{get;set;}
}

在这种情况下,我通常有一个实现IRepository的基本repo类,它被键入一个基本模型类

public interface IRepository<T> where T : IModel
 {
    void GetAll<T>();
    void GetById<T>(int id);
 }    

 public interface IEmployeeRepo<T> : IRepository<T> where T : IModel
 {
    void DoSomethingEmployeeRelated();
 }

 public class BaseRepo : IRepository<T> where T : IModel
 {

    public void GetAll<T>()
    {

    }

    public void GetById<T>(int id)
    {

    }
 }


 public class EmployeeRepo : BaseRepo<Employee>,  IEmployeeRepo<Employee>
 {
    public void DoSomethingEmployeeRelated()
    {

    }

 }

 //My example model class
 public class Employee : IModel
 {
     public int Id {get;set;}
     public string Name {get;set;}
 }
公共接口IRepository,其中T:IModel
{
void GetAll();
无效GetById(int-id);
}    
公共接口IEEmployeeRepo:IRepository,其中T:IModel
{
void DoSomethingEmployeeRelated();
}
公共类BaseRepo:IRepository其中T:IModel
{
public void GetAll()
{
}
public void GetById(int id)
{
}
}
公共类EmployeeRepo:BaseRepo、ieemployeerepo
{
公共无效DoSomethingEmployeeRelated()
{
}
}
//我的示例模型类
公共类雇员:IModel
{
公共int Id{get;set;}
公共字符串名称{get;set;}
}

不确定为什么存储库中有两个类型参数-重点是什么

*下面是使用泛型的.NET存储库的经典示例:*

*首先,存储库接口:*

公共接口i假设,其中T:class
{
T FindSingle(表达式谓词);
IQueryable FindAll();//可选-偏好问题
无效添加(T实体);
无效删除(T实体);
}
*第二,通用存储库实现(以EF为例):*

公共抽象类GenericRepository:IRepository
{
private IObjectSet _ObjectSet;//通过DI将其输入(例如)
公共T FindSingle(表达式>谓词)
{
返回_ObjectSet.SingleOrDefault(谓词);
}
//您可以了解如何执行其他实现方法
}
*然后是特定的存储库(每个聚合根应该有一个存储库,每个特定的存储库也应该有一个接口,详细说明特定的方法):*

public EmployeeRepository:genericpository,IRepository
{
//继承的所有常规方法(查找、添加、删除)-利用它们
公共员工FindEmployeeByName(字符串名称)
{
返回FindAll().SingleOrDefault(x=>x.Name==Name);
//或者您可以这样做:返回FindSingle(x=>x.Name==Name);
}
}
用法:

IRepository<Employee> repository = new EmployeeRepository<Employee>();
IRepository repository=new EmployeeRepository();
不要过分热衷于泛型——您唯一需要的是将存储库约束为封装在存储库后面的实体所使用

我只是使用
其中T:class


其他人使用
where T:IDomainAggregate
或类似方法,对允许的实体的实际类型施加约束。

您的代码中有太多错误(我至少可以发现3个)了解你想要什么。阅读上面的注释,我还提到它不会编译。我问你,你是从什么地方得到这个文档的还是你自己写的?
public abstract class GenericRepository<T> : IRepository<T>
{
   private IObjectSet<T> _ObjectSet; // get this in via DI (for example)

   public T FindSingle(Expression<T,bool>> predicate)
   {
      return _ObjectSet.SingleOrDefault(predicate);
   }

   // you can figure out how to do the other implementation methods
}
public EmployeeRepository : GenericRepository<Employee>, IRepository<Employee>
{
   // all regular methods (Find, Add, Remove) inherited - make use of them
   public Employee FindEmployeeByName(string name)
   {
      return FindAll().SingleOrDefault(x => x.Name == name);
      // or you could do: return FindSingle(x => x.Name == name);    
   }
}
IRepository<Employee> repository = new EmployeeRepository<Employee>();