liveBook · Manning

Hexagonal architecture

As explained in AI-Powered Developer: Build software with ChatGPT and Copilot

Hexagonal architecture is an architectural pattern that isolates an application’s core business logic from external systems through ports and adapters.

Hexagonal architecture, or the Ports and Adapters Pattern, separates an application’s core business logic from databases, user interfaces, and third-party services. The domain model holds business rules, entities, value objects, and domain services without depending on implementation details such as APIs or persistence technology. This keeps the core focused on correctness and workflows rather than the mechanics of the outside world. section 3.3 section 4.1

The boundary is expressed through ports: interfaces that define contracts between the domain and external systems. Primary ports define the use cases exposed to outside systems; secondary ports define what the application expects from repositories or external services. Adapters implement those contracts and translate external data and protocols into forms the domain can use. section 3.3 section 4.3.1

Use the pattern to make external concerns replaceable while protecting the core:

  • Keep business rules and workflows in the domain, independent of UI, database, and service implementation details. section 4.1
  • Expose application behavior through ports rather than letting outer systems reach directly into the core. section 3.3 section 4.3.1
  • Put concrete integration work in adapters, so a changed database, screen, or external service can be addressed by updating its adapter. section 4.3.1
  • Document the resulting structure with class, C4, and project-layout diagrams; inspect generated designs rather than accepting them automatically. section 3.3 section 3.4

IT asset management application

An IT asset management system can place Hardware, Asset, FundingDetails, and related business services in its domain. A FastAPI adapter can handle REST interaction, while a SQLAlchemy adapter manages persistence. The proposed layout separates adapters, domain code, infrastructure, and tests, making the external framework choices visible without putting them at the center of the model. section 3.3 section 3.3

I would like to build an ITAM project written in Python. It will focus on the tracking and management of Hardware. It should expose REST APIs, using FastAPI, and persist data using SQLAlchemy. It should use hexagonal architecture. As a software architect, please show me the Mermaid class diagram for this project."

Use this prompt as a starting point for producing a Mermaid class diagram for an IT asset management design with explicit Python, FastAPI, SQLAlchemy, and hexagonal-architecture constraints. section 3.3

The pattern supports maintainability and flexibility, but the boundary alone does not validate a design. Treat generated diagrams, package structures, and implementation suggestions as drafts: refine requirements, review the separation of responsibilities, and test adapters and domain services independently. section 3.3 section 3.4

How AI-Powered Developer: Build software with ChatGPT and Copilot covers it

How deep it goes
Defines domain models, primary and secondary ports, and adapters, then applies them in a Python project layout without implementation internals.
Examples
Uses an IT asset management system with Hardware, FastAPI, SQLAlchemy, Mermaid class diagrams, package structure, and tests.
What it assumes
Assumes familiarity with application code and design patterns while defining the domain, ports, adapters, and external-system boundary.
What you can do after
Evaluate/understand how ports and adapters let external databases, screens, and services change without changing core business logic.
How it connects
Connects domain model, REST APIs, databases, SOLID principles, C4 model, Repository Pattern, and Adapter Pattern.

Also explained in

Compare how 6 books explain hexagonal architecture →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of AI-Powered Developer: Build software with ChatGPT and Copilot. The section links point to the parts of the book it draws on.