5

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?

Doc Brown
219k35 gold badges406 silver badges620 bronze badges
asked Feb 6, 2020 at 2:28
2
  • 8
    Enum and other types are part of the interface. Commented Feb 6, 2020 at 2:40
  • @Fabio Ok thanks. Couldn't find this specifically mentioned in my book unfortunately, but it makes sense. Commented Feb 6, 2020 at 2:45

2 Answers 2

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.

answered Feb 6, 2020 at 18:49
0

The core idea of DIP is to decouple components. Enums and custom types that are essential for the interface can be part of the same assembly as the interface itself. This maintains a clear separation of concerns and ensures that clients only depend on the abstraction, not the implementation details.

You can also, use a a separate assembly (e.g. CommonTypes) for the status, that contains common types shared across different components. Both the client and service can reference the CommonTypes assembly to access the Status enum without violating DIP.

Both implementation makes sense from the dependency inversion principle point of view, and if you want to extend the the functionality(open-close principle) any implementation would allow adding a new implementation of the module.

I would opt for the second option in this case, as I consider it's more decoupled, so in that case your client relies on the Status type. Status as a type is more abstract than the interface, and can be used in more interfaces.

answered Aug 18, 2023 at 16:16

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.