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

Jon*_*Jon 5 c# generics inheritance repository c#-4.0

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

我想要类似下面的东西,但不能完全得到它编译.

 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;}
 }
Run Code Online (Sandbox Code Playgroud)

RPM*_*984 18

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

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

*首先,存储库接口:*

public interface IRepository<T> where T : class
{
   T FindSingle(Expression<Func<T,bool>> predicate);
   IQueryable<T> FindAll(); // optional - matter of preference
   void Add(T entity);
   void Remove(T entity);
}
Run Code Online (Sandbox Code Playgroud)

*其次,通用存储库实现(以EF为例):*

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
}
Run Code Online (Sandbox Code Playgroud)

*然后,特定存储库(每个聚合根应该有一个,每个特定存储库的接口也详细说明特定方法):*

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);    
   }
}
Run Code Online (Sandbox Code Playgroud)

用法:

IRepository<Employee> repository = new EmployeeRepository<Employee>();
Run Code Online (Sandbox Code Playgroud)

不要因为泛型而过于疯狂 - 你需要的唯一一个就是限制存储库被封装在存储库后面的实体使用.

我只是用where T : class.

其他用途where T : IDomainAggregate或类似用途,对实际允许的实体类型设置约束.


Tim*_*imC 5

在这种情况下,我通常有一个实现IRepository <>的基础repo类,并输入到基础Model类.

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;}
 }
Run Code Online (Sandbox Code Playgroud)