I am writing two APIs that I will use with many of my projects. In some projects, I use one of the APIs and the other, but most of my projects will use both. I try to design them as if they are completely separate, but I am fighting for one thing.
namespace FirstApi {
public abstract class MyBaseClass {
public IEnumerable<T> Search<T>() where T : MyBaseClass, new() {
}
}
}
namespace SecondApi {
public interface IMyInterface {
IEnumerable<T> Search<T>() where T : IMyInterface, new();
}
}
namespace MyProject {
public class MyDerivedClass : MyBaseClass, IMyInterface {
}
}
Both APIs require this search method. The second API has some functions in other classes that call IMyInterface.Search<T>(), and I would like those classes that inherit MyBaseClassto use the function Search<T>defined in MyBaseClass.
:. "T" "MyBaseClass.Search()" "T" "IMyInterface.Search()", .
. T , . , # 2.0 (# return ), !
, , ?
:
, , API.
public abstract class ApiAdapter<TAdapter> : MyBaseClass, IMyInterface where TAdapter: MyBaseClass, IJsonObject, new()
{
IEnumerable<T> IJsonObject.Search<T>()
{
foreach (TAdapter row in base.Search<TAdapter>())
yield return (T)(IMyInterface)row;
}
}
.
public class Client : ApiAdapter<Client> {
}