-1

I have 2 interfaces that have many of the same methods and it seems to break the DRY principle. How can I get it where I don't have these duplicated methods in my interfaces? or does it not matter for interfaces?

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

Interface 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);
}
Ňɏssa Pøngjǣrdenlarp
  • 38,411
  • 12
  • 59
  • 178
djblois
  • 963
  • 1
  • 17
  • 52
  • Why can't the first interface inherit (extend) the second? What have you tried? What _specifically_ can't you figure out? – Peter Duniho Nov 02 '17 at 01:19
  • @PeterDuniho, I am just learning about interfaces. Right now I just use them because I was told in a video to use them. I am trying to fully understand them. From what I understand because of `where Tentity : Class` in my code then I cannot inherent from another interface? am I not right? Because if I put another : I just get an error – djblois Nov 02 '17 at 01:22
  • _"From what I understand because of where Tentity : Class in my code then I cannot inherent from another interface? am I not right?"_ -- you are not right. If you search Stack Overflow for the text of the error message you're getting, you'll probably find out how to accomplish that (hint: generic constraint). – Peter Duniho Nov 02 '17 at 01:25
  • While there is duplication, I wouldn't necessarily apply DRY just yet as that can get you in trouble too. I would keep it as is and if it shows up again in another interface I would think about your design. One way to break it up would be to only point the common pieces in one interface and implement both on your class. – Jamie Nov 02 '17 at 01:26
  • 1
    Note, you shouldn't put optional parameters in interface methods. It's not that you can't, but it will introduce some inconsistency that can't be avoided. https://stackoverflow.com/q/4922714/491907 – pinkfloydx33 Nov 02 '17 at 08:28

2 Answers2

0

You could perhaps segregate the IRepositoryBase into multiple smaller interfaces. For instance you could have the following: -

public interface UnitOfWork<TEntity> : IDisposable
  where TEntity : class
{
    void Commit();
}

public interface IDeletableRepositoryBase<TEntity> : IDisposable
  where TEntity : class
{
    void Delete(object id);
    void Delete(TEntity entity);
}

public interface IGetRepositoryBase<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 IUpsertRepositoryBase<TEntity> : IDisposable
  where TEntity : class
{
    void Insert(TEntity entity);
    void Update(TEntity entity);
}

public interface IThirdWaveRepositoryBase<TEntity> : IDisposable, IGetRepositoryBase<TEntity>
  where TEntity : class
{

}
pmcilreavy
  • 3,076
  • 2
  • 28
  • 37
0

I would suggest something like this:

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

The void Dispose(); member has disappeared because of the IDisposable interface addition.

Enigmativity
  • 113,464
  • 11
  • 89
  • 172