Internal interface leads to DRY violation

100 Views Asked by At

I have the following:

internal class Person {
}

internal interface IGetPerson {
  Person GetPerson();
}

public class Cat: IGetPerson {
  private Person _Person = new Person();

  Person IGetPerson.GetPerson() {
    return _Person;
  }

  internal Person GetPerson() {  // dry violation -- necessary?
    return _Person; // or return (this as IGetPerson).GetPerson();     
  }
}

It appears to be necessary to get the following to compile, without an "as" cast:

internal class SomeClass {
  public static Person GetPerson(Cat someCat) {
    return someCat.GetPerson();
  }
}

The upshot is that unless I'm missing something, adopting an internal interface will inevitably lead to this kind of DRY violation. The alternative is to make the Person class public.

Am I missing something?

1

There are 1 best solutions below

2
On

In this case, you don't actually need two methods. Just take out the "IGetPerson." (including the point) from the first override, then take out the second method. If you are using VS 2015's quick actions, try selecting "Implement interface" instead of "Implement interface explicitly."