【发布时间】:2011-04-22 03:39:27
【问题描述】:
我正在尝试为我的所有存储库实现定义一个特定接口,而不仅仅是这样:
public abstract class GeneralizedRepository
{
readonly IDataModel _Model;
public GeneralizedRepository(IDataModel Model) {
if (Model == null)
throw new NullReferenceException();
_Model = Model;
}
public IDataModel DataModel { get { return _Model; } }
public abstract IEnumerable<T> GetAll<T>();
public abstract T GetOne<T>(Func<T, bool> predicate);
public abstract bool Contains<T>(Func<T, bool> predicate);
public abstract void Add<T>(T entity);
public abstract void Update<T>(T entity);
public abstract bool Remove<T>(Func<T, bool> predicate);
}
这是一个类,因为我的派生存储库具有共同的实例 DataModel
现在我有具体的实现:
public class DetailRep : GeneralizedRepository
{
public DetailRep(IDataModel Model) : base(Model) { }
public DetailRep(UnitOfWork Unit) : base(Unit.Model) { }
public override IEnumerable<T> GetAll<T>() {
throw new NotImplementedException();
}
public override T GetOne<T>(Func<T, bool> predicate) {
throw new NotImplementedException();
}
public override bool Contains<T>(Func<T, bool> predicate) {
throw new NotImplementedException();
}
public override void Add<T>(T entity) {
throw new NotImplementedException();
}
public override void Update<T>(T entity) {
throw new NotImplementedException();
}
public override bool Remove<T>(Func<T, bool> predicate) {
throw new NotImplementedException();
}
}
但它是一个Detail Repository,我的意思是我想替换所有T类型的Detail
但是编译时抛出错误:
类型参数声明必须是标识符而不是类型
【问题讨论】:
-
题目中的类型根本没有出现在题目中!?你能再检查一下你的问题吗?并提供有关错误的更多详细信息。例如产生错误的行?对 base(...) 的调用是错误的,但在解释之前,我首先需要了解您真正想要的是什么。
标签: .net generics repository