Genericising a repository interface

173 views Asked by At

Can anyone suggest a way to genericise the following interfaces so I can have one single interface rather than one per repository.

public interface IClass1Repository
{
    Class1 NewEntity();
    Class1 AddOrUpdate(Class1 entity);
    Class1 GetById(int Id);
    Class1 GetByNavigation(string NavigationString);
    IQueryable<Class1> All();
}

public interface IClass2Repository
{
    Class2 NewEntity();
    Class2 AddOrUpdate(Class2 entity);
    Class2 GetById(int Id);
    Class2 GetByNavigation(string NavigationString);
    IQueryable<Class2> All();
}

public interface IClass3Repository
{
    Class3 NewEntity();
    Class3 AddOrUpdate(Class3 entity);
    Class3 GetById(int Id);
    Class3 GetByNavigation(string NavigationString);
    IQueryable<Class3> All();
}


public interface IClass4Repository
{
    Class4 NewEntity();
    Class4 AddOrUpdate(Class4 entity);
    Class4 GetById(int Id);
    Class4 GetByNavigation(string NavigationString);
    IQueryable<Class4> All();
}
2

There are 2 answers

0
logicnp On BEST ANSWER

Try this:

public interface IRepository<T>
{
    T NewEntity();
    T AddOrUpdate(T entity);
    T GetById(int Id);
    T GetByNavigation(string NavigationString);
    IQueryable<T> All();
}
0
Burt On

Would something like this do it:

public interface IRepository<T>
{
    T NewEntity();
    T AddOrUpdate(T entity);
    T GetById(int Id);
    T GetByNavigation(string NavigationString);
    IQueryable<T> All();
}