我正在尝试实现最佳的代码可重用性。问题是我无法通过存储库访问位于主程序的基本抽象类中的基本方法。
如果您浏览下面的示例,您将看到我的情况的示例代码。
所以我的问题是如何从主程序访问位于基本抽象类中的方法。
类/接口
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>();
//
}
}
//主应用
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);
}
}