Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ports and adapters / hexagonal architecture - clarification of terms and implementation

After reading different sources about the Ports & Adapters architecture including Alistair Cockburn's original article I am still not sure about the definite meaning of the terms "port" and "adapter" - especially when it comes to mapping these concepts to implementation artifacts.

Several sources (e. g. this post) imply that the ports in this architecture pattern are artifacts at the very outside, followed by the adapters in the intermediate layer that translate between the ports and the application that is at the heart.

However, in Cockburn's original article the ports appear at the outside as well as on the inside of the adapter layer dependent on the direction of communication:

  • Inbound communication: "As events arrive from the outside world at a port, a technology-specific adapter converts it into a usable procedure call or message and passes it to the application."
  • Outbound communication: "When the application has something to send out, it sends it out through a port to an adapter, which creates the appropriate signals needed by the receiving technology (human or automated)."

Actually for me neither the "all outside" approach nor the "inside and outside" approach make sense - I would see the ports as artifacts that are always placed next to the application regardless of the direction of communication. Imo this would also be consistent with the port and adapter metaphors: E. g. having a device with a serial port, to connect another device without a serial port to this I'd need an adapter that adapts inbound and outbound communication from the viewpoint of my device.

Coming to the implementation of this architecture I'd see the definition of the ports rather as a part of my application where I'd see the different adapters as being "outside" of my application. E. g. an implementation of a single port could consist of a facade (to be called by adapters for inbound communication) and an interface (to be implemented by adapters for outbound communication).

What is the correct meaning of the terms port and adapter and how can these concepts be mapped to implementation artifacts?

UPDATE:

Found this article which resembles my understanding. The question remains if there is some kind of common agreement.

like image 950
lost Avatar asked Apr 15 '14 10:04

lost


People also ask

What are ports and Adapters in hexagonal architecture?

The hexagonal architecture, or ports and adapters architecture, is an architectural pattern used in software design. It aims at creating loosely coupled application components that can be easily connected to their software environment by means of ports and adapters.

What is the difference between port and adapter?

An example of a secondary port is an interface to store single objects. This interface simply specifies that an object be created, retrieved, updated, and deleted. It tells you nothing about the way the object is stored. An adapter is a bridge between the application and the service that is needed by the application.

What is a port in software architecture?

It inserts ports between your controller and your application, as well as between your application and your database adapter or ORM. A port is a metaphor for an Operating System port. In this example, a port is simply an interface.

Why is it called hexagonal architecture?

Imagine a hexagon shape with another, larger hexagon shape around it. The center hexagon is the core of the application (the business and application logic). The layer between the core and the outer hexagon is the adapter layer. And each side of the hexagon represents the ports.


1 Answers

inf3rno gave a good answer which clarifies the original question, but it may be useful to highlight some other uses for ports and adapters.

According to my understanding the port is an expression of your interface.

The port:

  • Defines the exposure of the core's functionality (for 'incoming' ports)
  • Defines the core's view of the outside world (for 'outgoing' ports)

The adapter:

  • Is located outside the component (hexagon)
  • Is used to ensure that the transport between port and the target happens in a way that satisfies the contract with the port's interface
  • Is what you replace (using dependency injection) to test the hexagon

The port should accept the adapter and make sure that the adapter implements the interface. Then it should merely call the appropriate methods/functions on the adapter.

The port should be included in communication testing. In that case, what is 'mocked out' is the cores of two neighbouring hexagons (or a hexagon and a service) and test the port/adapter/adapter/port assembly.

For more information you can view the talk about Hexagonal Microservices that James Gardner and I gave at London's Skillsmatter Microservices meetup in July 2014.

like image 66
Vlad Mettler Avatar answered Sep 28 '22 01:09

Vlad Mettler