|
| 1 | +# Hexagonal Architecture Example |
| 2 | + |
| 3 | +This example demonstrates how to use Pacta to enforce [Hexagonal Architecture](https://alistair.cockburn.us/hexagonal-architecture/) (also known as Ports and Adapters). |
| 4 | + |
| 5 | +## Architecture Overview |
| 6 | + |
| 7 | +```mermaid |
| 8 | +flowchart TB |
| 9 | + subgraph Driving["Driving Side (Primary)"] |
| 10 | + PA[Primary Adapters<br/>Controllers, CLI, Event Handlers] |
| 11 | + end |
| 12 | +
|
| 13 | + subgraph Application["Application Core"] |
| 14 | + IP[Inbound Ports<br/>Use Case Interfaces] |
| 15 | + subgraph Domain["DOMAIN"] |
| 16 | + D[Entities<br/>Domain Services] |
| 17 | + end |
| 18 | + OP[Outbound Ports<br/>Repository Interfaces] |
| 19 | + end |
| 20 | +
|
| 21 | + subgraph Driven["Driven Side (Secondary)"] |
| 22 | + SA[Secondary Adapters<br/>Database, APIs, Queues] |
| 23 | + end |
| 24 | +
|
| 25 | + PA -->|uses| IP |
| 26 | + IP -->|calls| D |
| 27 | + D -->|uses| OP |
| 28 | + SA -.->|implements| OP |
| 29 | +
|
| 30 | + style Domain fill:#e3f2fd,stroke:#1565c0,stroke-width:2px |
| 31 | + style D fill:#bbdefb,stroke:#1565c0 |
| 32 | + style IP fill:#fff8e1,stroke:#f57f17 |
| 33 | + style OP fill:#fff8e1,stroke:#f57f17 |
| 34 | + style PA fill:#f3e5f5,stroke:#7b1fa2 |
| 35 | + style SA fill:#e8f5e9,stroke:#2e7d32 |
| 36 | +``` |
| 37 | + |
| 38 | +## Directory Structure |
| 39 | + |
| 40 | +``` |
| 41 | +src/ |
| 42 | +├── domain/ # Core business logic (center of hexagon) |
| 43 | +│ ├── product.py # Domain entity |
| 44 | +│ └── product_service.py # Domain service |
| 45 | +│ |
| 46 | +├── ports/ |
| 47 | +│ ├── inbound/ # Driving ports (use case interfaces) |
| 48 | +│ │ └── catalog_use_case.py |
| 49 | +│ └── outbound/ # Driven ports (repository interfaces) |
| 50 | +│ └── product_repository.py |
| 51 | +│ |
| 52 | +└── adapters/ |
| 53 | + ├── primary/ # Driving adapters (controllers, CLI) |
| 54 | + │ └── api_controller.py |
| 55 | + └── secondary/ # Driven adapters (database, APIs) |
| 56 | + └── postgres_product_repository.py |
| 57 | +``` |
| 58 | + |
| 59 | +## Key Rules |
| 60 | + |
| 61 | +| Rule | Description | |
| 62 | +|------|-------------| |
| 63 | +| Domain → Adapters | **Forbidden** - Domain must not know about adapters | |
| 64 | +| Domain → Outbound Ports | **Allowed** - Domain uses repository interfaces | |
| 65 | +| Ports → Adapters | **Forbidden** - Ports are interfaces, adapters implement them | |
| 66 | +| Primary Adapters → Domain | **Warning** - Should go through inbound ports | |
| 67 | +| Secondary Adapters → Outbound Ports | **Allowed** - Implements the interface | |
| 68 | +| Adapters → Adapters | **Forbidden** - Adapters should be independent | |
| 69 | + |
| 70 | +## Usage |
| 71 | + |
| 72 | +```bash |
| 73 | +# Run architecture check |
| 74 | +pacta scan . --model architecture.yml --rules rules.pacta.yml |
| 75 | + |
| 76 | +# Expected output (clean architecture): |
| 77 | +# ✓ 0 violations |
| 78 | +``` |
| 79 | + |
| 80 | +## Dependency Flow |
| 81 | + |
| 82 | +Dependencies always point **inward** toward the domain: |
| 83 | + |
| 84 | +```mermaid |
| 85 | +flowchart LR |
| 86 | + PA[Primary<br/>Adapters] --> IP[Inbound<br/>Ports] --> D((DOMAIN)) |
| 87 | + SA[Secondary<br/>Adapters] --> OP[Outbound<br/>Ports] --> D |
| 88 | +
|
| 89 | + style D fill:#e3f2fd,stroke:#1565c0,stroke-width:2px |
| 90 | +``` |
| 91 | + |
| 92 | +This ensures: |
| 93 | +- Domain is isolated and testable |
| 94 | +- Adapters can be swapped without changing business logic |
| 95 | +- The application is framework-agnostic |
0 commit comments