2011-01-24 115 views
5

我已阅读了一些关于约束的信息,并试图在我的存储库模式中实现它。存储库模式的通用接口继承和类实现

我想下面的东西,但不能完全得到它编译。

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;} 
} 
+0

你的代码中有太多的错误(我至少可以发现3个)来理解你想要的。 – leppie 2011-01-24 11:34:41

+0

阅读上面的注释IEmployeeRepo,我也提到它不会编译 – Jon 2011-01-24 11:35:52

回答

16

不知道为什么你在存储库上有两个类型参数 - 有什么意义?

* 下面是一个使用泛型一个.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); 
} 

* 其次,通用仓库实现(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 
} 

* 然后,特定信息库(你应该有每聚合根之一,并且还为每个特定存储库详细说明特定的方法的接口):*

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>(); 

不要出于对泛型的疯狂 - 你唯一需要的是限制Repository被封装在Repository后面的实体使用。

我只是使用where T : class

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

1

试试这个;

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;} 
} 
5

在这种情况下我通常有一个实现IRepository <>和键入到基模型类的基回购类。

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;} 
}