That's a lot of words. I'm going to restate what I think you're asking and answer that question instead. If I'm off the mark, let me know.
When invoking a method through an interface, does it matter if that interface is explicitly declared again as being implemented on a more derived type in the type hierarchy?
Yes, this is called "interface re-implementation" and it changes how the method is mapped. The C# language specification (Section 13.4.6 Interface Re-Implementation) goes into this in a little bit more detail, but the gist is that the most derived type that specifies that interface is the starting point for lookup.
interface ICreature
{
void Speak();
}
class Animal : ICreature
{
public void Speak() { Console.WriteLine("Rawr"); }
}
class Duck:Animal
{
public void Speak() { Console.WriteLine("Quack"); }
}
class Human : Animal, ICreature
{
public void Speak() { Console.WriteLine("Hello"); }
}
If you do the following, it will print out "Rawr" and "Hello".
ICreature duck = new Duck();
ICreature human = new Human();
duck.Speak();
human.Speak();
This is because in the Duck hierarchy, the most derived type specifying the ICreature interface is Animal and so it will print out "Rawr".
In the Human hierarchy, the most derived type specifying the ICreature interface is Human (and Human declares an implementation) and so it will print out "Hello". If the Human type had not declared an implementation, it would also have printed "Rawr".
Update
In your specific case, the exact same rule applies. Let's walk through the steps.
- ICompanyRepository inherits from IRepository
- CompanyRepository declares that it implements ICompanyRepository
- CompanyRepository has now implicitly redeclared that it implements IRepository because ICompanyRepository inherits from IRepository
The call chain then follows these steps.
- The
Add()
method is called through the instance typed to the ICompanyRepository interface. - The most derived type that has explicitly declared that it implements IRepository is now CompanyRepository, so lookup begins there.
- CompanyRepository does not implement the Add() method directly, so its parent class is inspected.
- SQLRepositoryBase is inspected and does not directly implement the method, so its parent class is inspected.
- RepositoryBase is inspected and it does implement the method, so that is the method that will be called.