C# 存储库模式接口-最佳实践?

C# 存储库模式接口-最佳实践?,c#,.net,repository-pattern,C#,.net,Repository Pattern,我正在学习存储库模式,我发现了许多代码示例,但都几乎相同,因此我对此表示怀疑,例如此设计: public interface IRepository<T> { void Add(T entity); void Update(T entity); void Delete(T entity); IList<T> GetAll(); } public interface I

我正在学习存储库模式,我发现了许多代码示例,但都几乎相同,因此我对此表示怀疑,例如此设计:

     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;}
     }
但不是这个:(显然)

我该怎么办?我必须在UoW中创建另一个属性并返回IRepository吗? 我必须为没有CRUD操作的每个存储库创建一个接口(例如IPostRepository)?具体的存储库必须一次从ePrepository类和接口继承(例如:类PostRepository:ePrepository、IPostRepository{})

你觉得怎么样


PD:请原谅我的英语不好。

如果您将IPostRepository更改为继承自IRepository,那么您只是在扩展接口曲面,因此不需要重新定义所有方法

例如,通过此更改:

public interface IRepository<T>
{
    void Add(T entity);
    void Update(T entity);
    void Delete(T entity);
    IList<T> GetAll();
}
public interface IPostRepository : IRepository<int>
{
    int GetComentCount();
}
public class EFRepository<T> : IRepository<T>
{
    public void Add(T entity) { Console.WriteLine("Works"); }
    public void Update(T entity) { /*implementation...*/ }
    public void Delete(T entity) { /*implementation...*/ }
    public IList<T> GetAll() { return null; }
}
public class PostRepository : EFRepository<int>, IPostRepository
{
    public int GetComentCount() { return 0; }
}

public interface IUnitOfWork
{

}

public class UnitOfWork : IUnitOfWork, IDisposable
{
    public IPostRepository PostRepository { get { return new PostRepository(); } }

    public void Dispose()
    {
        throw new NotImplementedException();
    }
}
基本上,您的PostRepository不需要重新实现Add/Update/Delete方法,因为该接口约定已经存在于基类EFRepository中,并且将被使用。IPostRepository将强制您只提供扩展接口合同

至于最佳实践,我认为没有一个好的解决方案。我尝试使用inherit方法,但我已经看到了很好的生产代码,它具有ReadOnly/Add/AddUpdate/etc.存储库接口用于组合


另外,我在示例中用int更改了Post类,以避免定义一个全新的类。

如果您使用
IPostRepository
inherit
ipository
,则它将起作用。注释与@BenRobinson相同。此外,如果您想获得最佳实践,请避免返回具体类,如List、return ICollection、IList、IEnumerable、IQueryable或其他接口。@braintechd您说得对,我将更新该代码。“具体存储库必须一次从eRepository类和接口继承吗?”-是,如果您希望它继承基本的
eRepository
实现。@BenRobinson您是对的,我删除了我以前的注释,那是一个lapsus。谢谢,谢谢。在我的例子中,IPostRepository应该是泛型的(IPostRepository:IRepository),因为我在一个单独的项目中有接口,而没有引用EF项目。
    var collection = UoW.PostRepository.GetAll();
public interface IRepository<T>
{
    void Add(T entity);
    void Update(T entity);
    void Delete(T entity);
    IList<T> GetAll();
}
public interface IPostRepository : IRepository<int>
{
    int GetComentCount();
}
public class EFRepository<T> : IRepository<T>
{
    public void Add(T entity) { Console.WriteLine("Works"); }
    public void Update(T entity) { /*implementation...*/ }
    public void Delete(T entity) { /*implementation...*/ }
    public IList<T> GetAll() { return null; }
}
public class PostRepository : EFRepository<int>, IPostRepository
{
    public int GetComentCount() { return 0; }
}

public interface IUnitOfWork
{

}

public class UnitOfWork : IUnitOfWork, IDisposable
{
    public IPostRepository PostRepository { get { return new PostRepository(); } }

    public void Dispose()
    {
        throw new NotImplementedException();
    }
}
UnitOfWork t = new UnitOfWork();
t.PostRepository.Add(1);