One comment correctly states that adding virtual Display () to your Result class violates the principle of single responsibility. That's right.
Wipe your question here: because you want to do things like this:
private IRepository[] _repositories;
... there is no way to avoid type checking at runtime. The compiler has no idea which return type-result-from-result will be returned. All he knows is that your repository returns the object obtained from the result.
On the other hand, if you use generics:
interface IRepository<T> where T : Result { T[] Search(string data); }
... at compile time, you will find out what type of Result subclass you have in mind, thereby eliminating the need for type checking and the long string of "if" statements that follow from the first approach.
If you can use generics, then you can do things like this:
interface IResultDisplayService<T> where T : Result { void Display(T result); }
So, I suppose, my question is: is it necessary to store an array of these repositories? What is the real world use case for?
source share