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:
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.
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.
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.
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.
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.
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:
The adapter:
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With