Skip to content

Instantly share code, notes, and snippets.

@maloufde
Created January 25, 2024 12:11
Show Gist options
  • Save maloufde/09c830901c129981add223f4fbf59b2b to your computer and use it in GitHub Desktop.
Save maloufde/09c830901c129981add223f4fbf59b2b to your computer and use it in GitHub Desktop.
Example - Hexagonal Design - Ports and Adapters
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@maloufde
Copy link
Author

maloufde commented Jan 25, 2024

Hexagonal Design

This is an example for a structured monolith (or sometimes called modulith) to decouple adapters and framework from
the domain via contracts.

Domain (dark blue)

May be divided into several subdomains and/or shared domains. In a monolith, each domain may also be separated into
the api and the internal logic and model, invisible for other subdomains. This enables to tear the monolith apart into
several microservices if appropriate.

Ports (Yellow)

As in real life, you can't walk always alone. You need help and food - but you don't want to be dependent from a
single supermarket. So you choose an abstraction and name it, e.g. Object Store, Feature Toggle,
Configuration, Template Engine, User Directory etc. These are visualized by the round Ports,
representing consumer contracts you are willing to fulfill (magenta), or you expect some other components to fulfill (light-blue).

Adapters, Frameworks and Dependencies (White / Green)

For Unit Testing you just need the Domain, the Ports and some test doubles.
For production, the Adapters (white) are components to adapt the application needs to the
surrounding service landscape.

Tips:

  • Use as few dependencies as possible in the domain layer.
  • Put framework and dependencies in the green layer - even simple Spring Boot application easily sum up to 70 MB or more
  • you may package domain and contracts together. Testing is easy - you don't even need Mocking frameworks, a simple Hashmap to simulate a store will often suffice.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment