我瞭解Repository模式,我發現許多代碼示例,但都是幾乎一樣的,所以我懷疑到這方面,例如這樣的設計:存儲庫模式接口 - 最佳實踐?
public interface IRepository<T>
{
void Add(T entity);
void Update(T entity);
void Delete(T entity);
IList<T> GetAll();
}
public interface IPostRepository
{
int GetComentCount();
}
public class EFRepository<T>: IRepository<T>
{
public void Add(T entity){ /*implementation...*/ }
public void Update(T entity){ /*implementation...*/ }
public void Delete(T entity){ /*implementation...*/ }
public IList<T> GetAll(){ /*implementation...*/ }
}
public class PostRepository: EFRepository<Post>, IPostRepository
{
public int GetComentCount(){ /*implementation...*/ }
}
public class UnitOfWork: IUnitOfWork, IDisposable
{
IPostRepository PostRepository {get;}
}
我可以這樣做:
IUnitOfWork UoW = new UnitOfWork();
int nComments = UoW.PostRepository.GetComentCount();
,但不是這個:(顯然)
var collection = UoW.PostRepository.GetAll();
我該怎麼辦?我必須在UoW中創建另一個屬性並返回一個IRepository嗎? 我必須爲每個存儲庫創建一個沒有CRUD操作的接口(例如IPostRepository)嗎?必須一個具體的存儲庫一次從EFRepository類和接口繼承(例如:類PostRepository:EFRepository,IPostRepository {})?
您認爲如何?
PD:請原諒我可憐的英語。
如果你讓'IPostRepository'繼承'IRepository',那麼它就會工作。 – 2014-10-03 15:16:55
與@BenRobinson相同的評論。此外,如果您要採取最佳做法,請避免返回列表,返回ICollection ,IList ,IEnumerable ,IQueryable 或其他某個接口。 –
braintechd
2014-10-03 15:18:31
@braintechd你是對的我會更新該代碼。 – AiApaec 2014-10-03 15:28:38