3

From what I understand two components A and B should only communicate with one another via an interface. Ideally this interface should be in its own separate assembly so that the client need not be loaded with the dependencies of any particular interface implementation.

The question then is how do I pass information such as enums and custom Types that appear within the interface if the client is only dependent on the interface?

Eg:

public enum Status
{
    Success,
    Failure
}

public interface SomeInterface
{
    Status CallAPI(string s);
}

In which assembly should Statusin this example reside so that both client and service can use the interface, but without breaking the principle of Dependency Inversion? Can it be contained within the same assembly as the stand-alone interface? What if the enum was replaced with a custom Type that the interface depended on?

2
  • 8
    Enum and other types are part of the interface. – Fabio Feb 6 '20 at 2:40
  • @Fabio Ok thanks. Couldn't find this specifically mentioned in my book unfortunately, but it makes sense. – user4779 Feb 6 '20 at 2:45
1

two components should only communicate with one another via an interface.

Interface includes not only some methods, but both return and argument types. In ideal engineering world it could be written as

public interface SomeInterface    {
  public enum Status {
      Success,
      Failure
  }
    Status CallAPI(string s);
}

so enum become part of interface.

But for many reasons (maintainability, readability etc), in general, it's bad idea to move enum inside interface. (In some cases, however, it's OK and I was working on projects with such structure). That's why best practice is to have different file with enum in same package as interface.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.