2011-04-21 50 views
0

我試圖定義一個特定的接口爲我所有的庫實現,比僅僅是這樣的:IRepository與IRepository <T>

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(); 
    } 
} 

但它是一個詳細庫,我的意思是,我要全部更換牛逼的該類型詳細

但錯誤是在編譯時拋出:

類型參數聲明必須是一個標識符不是一個類型

+0

問題的標題中的類型沒有出現在問題中!?你能再次檢查你的問題嗎?並提供有關錯誤的更多詳細信息。例如,產生錯誤的行?對基地(...)的呼叫是錯誤的,但在解釋之前,我首先需要了解你真正想要的。 – Achim 2011-04-21 19:33:27

回答

2

聲明你的抽象類作爲通用:

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); 
} 

而不是在你的派生類FIL l與您的類型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){} 
} 
+0

但以這種方式,如果我有10個存儲庫,並且每個實現GeneralizedRepository ,其中T是內容的類型比存儲庫包含的編譯器將生成GeneralizedRepository ,GeneralizedRepository ,.... GeneralizedRepository 。我只想要一個抽象類和10個派生類。 – anotherNeo 2011-04-21 21:05:13