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 overr开发者_开发技巧ide 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
Declare your abstract class as a generic:
public abstract class GeneralizedRepository<T>
{
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();
public abstract T GetOne(Func<T, bool> predicate);
public abstract bool Contains(Func<T, bool> predicate);
public abstract void Add(T entity);
public abstract void Update(T entity);
public abstract bool Remove(Func<T, bool> predicate);
}
And than on your derived class fill in the Type with your Detail
:
public class DetailRep : GeneralizedRepository<Detail>
{
public DetailRep(IDataModel model)
: base(model)
{
}
public override IEnumerable<Detail> GetAll(){}
public override Detail GetOne(Func<Detail, bool> predicate){}
public override bool Contains(Func<Detail, bool> predicate){}
public override void Add(Detail entity){}
public override void Update(Detail entity){}
public override bool Remove(Func<Detail, bool> predicate){}
}
精彩评论