preface

Note: When I was learning DDD, I saw the six-sided architecture and was deeply attracted by it after I had a preliminary understanding of its architectural ideas, because it could solve many problems I encountered in the project.

Here we will focus on the idea of hexagonal architecture and the problems it solves without making in-depth comparisons with other architectures.

Hexagonal architecture, also known as ports&adpers(which I think is more appropriate), was proposed by Cockburn in 2005 to decouple business logic from input and output.

Architecture thought

Imagine how computers interact with peripheral devices:

Peripheral devices include earphones, keyboards, external monitors, earphones, etc., which are connected through USB and HDMI ports of computers. But sometimes just bought a new device (such as printer), want to connect with the computer through USB, but when you plug in, the computer prompts you can not identify, need to install the driver, you follow the instructions, install the driver can be used. And the amazing thing is that I have two mice, one is PS /2 port, the other is USB port, but it works because the system has two types of interface drivers (adapters) installed.

From the above description, we know that computers and peripheral devices interact with drivers (adapters) through ports, and only need corresponding drivers, ports can interact with different devices.

We often make the mistake of intertwining business logic with interactions between external entities in our design architecture and coding. For example, in the hierarchical architecture, the access to the database and the access to the external interface is usually placed in the Gateway. However, because of the hierarchical architecture, the job division of the business is only done, and the decoupling of the hierarchy is not done, resulting in the access to the external becomes the core of the business logic. The same is true for externally provided interfaces encapsulated in controllers. (Of course, this is not to say that layered architecture is bad, just that it is approached from a different Angle).

Hexagonal architecture focuses on the difference between “external” and “internal”. Internal business logic (Application) is completely isolated from peripherals (APP,WEB, database, etc.) and only interacts with Adapter.

So what does the Adapter do? It is responsible for converting data interacting with peripherals (including commands and queries) into information (business Module) that can be understood by Application, and processing business logic through interfaces provided by the internal system.

With all that said about ADPTER, how does port define its responsibilities? As we know, the port on the computer defines the communication protocol of the device. As long as it is the same port, no matter what device must follow this protocol, but the content of its communication may be different. For a software system, the embodiment of port protocol on port is API, which is the interface exposed by the business system. A port can have multiple adapters. For example, a system provides the display of production information, which may need to provide Product information externally in APP, Web or as a remote service. In this case, Application provides a Query interface and returns a Product object. To convert it to APP, Web, or remote service JSON format, adapter takes care of that.

Why is it called a hexagon? One side of a hexagon represents a port, but it does not mean that a hexagon architecture must have six ports. In fact, this has nothing to do with six sides. The author just drew the architecture as a hexagon for the convenience of expressing his ideas, so that users can design the business architecture more conveniently.

I’d like to remind you that although hexagon emphasizes the difference between internal and external, it doesn’t mean we don’t care about the active and passive of external interfaces (providing services externally or calling external services). However, we can use technology to shield such differences when implementing ports and adapters, but it is not suitable. There is no need to use an identical port to mask the difference. Therefore, the normal hexagonal architecture will have ports on the left and right sides, with the left side representing external services and the right side representing external services.

From the above introduction, I believe you have already understood the design idea of the hexagon. What else does this separation of business logic and external systems give us?

As shown in the figure above, the out of application pair provides two ports, the user-side API and the data-side API. User side API, through four adapters for APP, HTTP,GUI, IE services; The data side provides adaptation services for applications through two adapters (DB and mock). You might be a little surprised to see this, but why mock?

Yes, any service that only needs to implement the data-side port can provide data to the Application, and the system will not be aware of the service difference. That is to say, mock and DB can provide the same service. At this point you understand that you no longer need to embed test cases into the business logic, just implement the appropriate Adapter, and never change the test cases because the business code changes. It provides great convenience for integrated testing.

I will add a project case later !!!!

http://alistair.cockburn.us/Hexagonal+architecture