I’m trying to implement the best code re-usability. The problem is that I can’t access the base method located in the Base Abstract class form the Main Program through the repository.
If you go through the example below you will see a sample code of my situation.
So my question is how can I access methods located in the base abstract class from the main program.
Classes/Interfaces
public abstract class BaseEntity
{
public override abstract String ToString();
}
public abstract class BaseClass<T> where T : BaseEntity
{
public T GetById(int id)
{
//Dummy Code
return new T();
//
}
}
public interface IFooRepository
{
IList<Foo> GetOrderedObjects();
}
public interface FooRepository : BaseClass<Foo>, IFooRepository
{
public IList<Foo> GetOrderedObjects()
{
//GetById method is accessible from the repository - Fine
var obj = this.GetById(5);
//Dummy Code
return new List<Foo>();
//
}
}
//Main App
public class void Main()
{
private IFooRepository _fooRepository;
public void ProgramStartsHere()
{
//This is ok.
var list = _fooRepository.GetOrderedObjects();
//Problem is here - GetById method is not accessible from the main program through the FooRepository
var obj = _fooRepository.GetById(10);
}
}
GetById isn’t defined in the interface
I would make an
Then
BaseClassimplementsIBaseRepository<T>and
IFooRepositoryinherits fromIBaseRepository<Foo>EDIT :
A full example, similar to @Olivier J-D one, with idea (maybe wrong), that GetOrderedObject may be same for all your entities.