0

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?

William Jockusch
  • 26,513
  • 49
  • 182
  • 323

1 Answers1

0

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."

mousetail
  • 7,009
  • 4
  • 25
  • 45
  • that won't build. Problem is that GetPerson has to be public to implement an interface, but can't be public because Person is not public. – William Jockusch Jul 07 '16 at 07:27
  • I see. You can either make the interface a static class, so it can have internal members, make "person" public, make Cat internal, or do as you suggested.In this case, the dry violation doesn't really matter since it's only one simple line, I wouldn't worry about it too much. – mousetail Jul 07 '16 at 09:30