Abstracting External Systems in Hexagonal Architecture

Discover the domain model refinement and abstraction within hexagonal architecture, which allows seamless interaction with external systems, enhancing testability and flexibility in application development.

In this lesson, we’ll consider some of the decisions we need to make when applying hexagonal architecture approach. We’ll take a step-by-step approach to handling external systems where we’ll first decide what the domain model needs, and then work out the right abstractions that hide their technical details.

We’ll consider two common external systems:

  • Web requests

  • Database access

Creating abstractions for external systems

The place to begin our design is with our domain model. We need to devise a suitable port for our domain model to interact with. This port has to be free from any details of our external system, and at the same time, it must answer the question of what our application needs this system for. We’re creating an abstraction.

A good way to think about abstractions is to consider what would stay the same if we changed how we performed a task. Suppose we want to eat warm soup for lunch. We might warm it in a pan on the stove or perhaps in the microwave. No matter how we choose to do it, what we are doing stays the same. We are warming the soup, and that is the abstraction we’re looking for.

We don’t often warm soup in software systems unless we’re building an automated soup vending machine. However, there are several common kinds of abstractions we’ll be using. This is because common kinds of external systems are used when building a typical web application. The first and most obvious is the connection to the web itself. In most applications, we’ll encounter some kind of data store, typically a third-party database system. For many applications, we’ll also be calling out to another web ...

Get hands-on with 1400+ tech skills courses.