如何处理2个几乎相同的接口?

时间:2017-11-02 01:17:02

标签: c# interface

我有2个接口,它们有许多相同的方法,似乎打破了DRY原则。我怎样才能在我的界面中找到这些重复的方法?或接口无关紧要?

接口1:

public interface IRepositoryBase<TEntity> where TEntity : class
{
    void Commit();
    void Delete(object id);
    void Delete(TEntity entity);
    void Dispose();
    IQueryable<TEntity> GetAll();
    IQueryable<TEntity> GetAll(object filter);
    TEntity GetById(object id);
    TEntity GetFullObject(object id);
    IQueryable<TEntity> GetPaged(int top = 20, int skip = 0, object orderBy = null, object filter = null);
    void Insert(TEntity entity);
    void Update(TEntity entity);
}

接口2:

public interface IThirdWaveRepositoryBase<TEntity> where TEntity : class
{
    IQueryable<TEntity> GetAll();
    IQueryable<TEntity> GetAll(object filter);
    TEntity GetById(object id);
    TEntity GetFullObject(object id);
    IQueryable<TEntity> GetPaged(int top = 20, int skip = 0, object orderBy = null, object filter = null);
}

2 个答案:

答案 0 :(得分:0)

您可以将IRepositoryBase隔离到多个较小的接口中。例如,您可以拥有以下内容: -

pmap_dfr

答案 1 :(得分:0)

我会建议这样的事情:

public interface IRepositoryReadOnly<TEntity> : IDisposable where TEntity : class
{
    IQueryable<TEntity> GetAll();
    IQueryable<TEntity> GetAll(object filter);
    TEntity GetById(object id);
    TEntity GetFullObject(object id);
    IQueryable<TEntity> GetPaged(int top = 20, int skip = 0, object orderBy = null, object filter = null);
}

public interface IRepository<TEntity> : IRepositoryReadOnly<TEntity> where TEntity : class
{
    void Delete(object id);
    void Delete(TEntity entity);

    void Insert(TEntity entity);
    void Update(TEntity entity);

    void Commit();
}

由于添加了void Dispose();接口,IDisposable成员已消失。