rasuru
rasuru

Reputation: 451

Role of Input Ports in Clean Architecture

Can you explain me what are the benefits of such abstraction as Input Ports in Clean Architecture? Why not use Interactors / Use Cases directly? I understand the role of Output Ports - this way use cases don't have to know about presenters and other adapters. But can't wrap my head around this one. In addition, is there any relation between repository interface and ports? Can repository interfaces be considered Input or Output ports? Thank you!

Upvotes: 13

Views: 3709

Answers (2)

hschaeufler
hschaeufler

Reputation: 186

This serves to implement the Open-Closed Principle.

A software artifact should be open for extension but closed for modification.

Read more about this in chapter 8 of Uncle Bob's Clean Architecture book. Here he writes in the subchapter Information Hiding:

Transitive dependencies are a violation of the general principle that software entities should not depend on things they don’t directly use. We’ll encounter that principle again when we talk about the Interface Segregation Principle and the Common Reuse Principle.

and further:

So, even though our first priority is to protect the Interactor from changes to the Controller, we also want to protect the Controller from changes to the Interactor by hiding the internals of the Interactor.

Upvotes: 7

alex_noname
alex_noname

Reputation: 32053

The Input and Output ports are nothing more than an application of the Dependency Inversion Principle of SOLID in clean Architecture. In most programming languages, they are interfaces (or abstract interfaces). If you look at the original diagram below.

You can see that Controller calls a method on Input Port interface, UseCase implements it, and then UseCase sends a response to the Output Port interface, which Presenter implements. In this case, all implementations of a part of the system can be replaced, since they depend only on the interfaces. enter image description here

Upvotes: 4

Related Questions