What is the difference between primary and secondary ports in Hexagonal Architecture

In hexagonal architecture, a primary port is a boundary interface that defines the interactions between the outside world and the application core, while a secondary port is a boundary interface that defines the interactions between the application core and any external collaborators that it depends on.

Posted by Gregory Pacheco on December 17, 2022

Primary Ports

In hexagonal architecture, a primary port is a boundary interface that defines the interactions between the outside world and the application core. These interactions are typically initiated by external actors, such as users or external systems. The primary port defines the contracts that these external actors must adhere to in order to interact with the application.

For example, in a web application, the primary port might be a set of REST APIs that allow external clients to retrieve data from the application or perform actions. In a desktop application, the primary port might be a set of GUI elements that allow the user to interact with the application.

Secondary Ports

On the other hand, a secondary port is a boundary interface that defines the interactions between the application core and any external collaborators that it depends on. These external collaborators might include external services, libraries, or frameworks that the application uses to perform certain tasks. The secondary port defines the contracts that the application core expects these external collaborators to adhere to in order to interact with it.

For example, in a web application, the secondary port might be a set of APIs that allow the application core to interact with an external database or messaging system. In a desktop application, the secondary port might be a set of APIs that allow the application core to interact with the operating system or other system-level functionality.

Overall, the primary port is the interface through which external actors interact with the application, while the secondary port is the interface through which the application core interacts with its external collaborators.