我对Generics非常陌生,并发现它在创建存储库时非常有用.我的问题必须是非常基本的水平.
假设,我已经声明了一个Generic接口
public interface IEFRepositorywhere T : class { int Count { get; } bool Contains(T entity); IQueryable GetAll(); IQueryable GetAll(string includeEntities); IQueryable FindBy(Expression > predicate, string includeEntities = ""); T Add(T entity); T GetById(int id); void Delete(T entity); void Update(T entity); int ExecuteSQL(string commandText); void Delete(int id); }
现在,我可以通过任何实现此合同的类.但是我想限制它仅被我的Config文件中配置的白名单继承
例如.允许"人员|部门|员工".
您可以在类型参数上使用约束,例如,将其限制为仅从其他类继承的类.
public interface IEFRepository<T> where T : MyBaseClass
http://msdn.microsoft.com/en-us/library/d5x73970.aspx