IRepository 与 IRepository
我试图为我的所有存储库实现定义一个特定的接口,而不仅仅是这样:
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 类型的所有 T
但在编译时会抛出错误:
类型参数声明必须是标识符而不是类型
I'm trying to define one specific interface for all my repositories implement, than is just this:
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);
}
This is a class because my derived repositories have in common the instance DataModel
Now I have concrete implementations as:
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();
}
}
But it is a Detail Repository, what i mean is that i want replace all T's for the type Detail
But an error is thrown at compile time:
Type parameter declaration must be an identifier not a type
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
将您的抽象类声明为泛型:
然后在派生类上用您的
详细信息
填写类型:Declare your abstract class as a generic:
And than on your derived class fill in the Type with your
Detail
: