I have a class similar to the following:
public abstract class Manager<T, TInterface> : IManager<T> where TInterface : IRepository<T>
{
protected abstract TInterface Repository { get; }
public virtual List<T> GetAll()
{
return Repository.GetAll();
}
}
This works perfectly fine, however, is there a way to get away from having the TInterface in the abstract class declaration and in the resulting class that extends my generic abstract class:
public class TestManager : Manager<TestObject, ITestRepository>, ITestManager
I am forced to use ITestRepository and make the Repository property abstract due to the fact that it can contain custom methods that I need to know about and be able to call.
As I continue to build layers, I will have to keep doing this process the whole way up the stack. Examples would be if I had a generic abstract controller or service layer:
public class TestService : Service<TestObject, ITestManager>, ITestService
Is there a better way to do this or is this the best practice to allow a generic class to call another generic class?
It seems that all you want to do is to make
Manager<T>testable, and use a mock as a repository that you can query for special members.If that’s the case, maybe you can change your design to this:
Now, all the specifics of testing are in a testing subclass:
When you write your unit tests, you create
TestingManager<T>instances (referenced throughTestingManager<T>declared variables and fields), and you provide them with a test repository. Whenever you query theirRepository, you’ll always get a strongly-typed test repository.UPDATE:
There’s another way to solve this, without a subclass. You declare your repository objects as test repositories that you pass to
Manager<T>s and you query them directly, without going through theManager<T>.