2011-01-24 82 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;} 
}