C#通用接口将候选者列入白名单过滤

Dip*_*hta 2 c# generics

我对Generics非常陌生,并发现它在创建存储库时非常有用.我的问题必须是非常基本的水平.

假设,我已经声明了一个Generic接口

public interface IEFRepository<T> where T : class
{
    int Count { get; }
    bool Contains(T entity);
    IQueryable<T> GetAll();
    IQueryable<T> GetAll(string includeEntities);
    IQueryable<T> FindBy(Expression<Func<T, bool>> 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);
}
Run Code Online (Sandbox Code Playgroud)

现在,我可以通过任何实现此合同的类.但是我想限制它仅被我的Config文件中配置的白名单继承

例如.允许"人员|部门|员工".

Osc*_*car 5

您可以在类型参数上使用约束,例如,将其限制为仅从其他类继承的类.

public interface IEFRepository<T>  where T : MyBaseClass
Run Code Online (Sandbox Code Playgroud)

http://msdn.microsoft.com/en-us/library/d5x73970.aspx