我对泛型非常陌生,发现它对案例存储库创建非常有帮助。 我的问题一定是非常基本的水平。
假设,我已经声明了一个类似的通用接口(interface)
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);
}
现在,我可以传递任何实现此契约的类。 但是我想限制它被我的Config文件中配置的Only white list Classes继承
例如。允许使用“人员 | 部门 | 员工”。
最佳答案
您可以对类型参数使用约束,例如,将其限制为仅继承自另一个类的类。
public interface IEFRepository<T> where T : MyBaseClass
关于C# 通用接口(interface)白名单候选过滤,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20797832/