Back to articles
Hexagonal Architecture in Python: Wiring Adapters, Dependency Injection, and the Application Layer

Hexagonal Architecture in Python: Wiring Adapters, Dependency Injection, and the Application Layer

via Dev.toPablo Ifrán

You have a domain model with real behavior. You have ports that define the boundaries. You have adapters that fulfill them. None of it runs until you wire it together. This post covers exactly that: the application layer in hexagonal architecture, how FastAPI's dependency injection connects concrete adapters to abstract ports , and the folder structure that keeps the dependency direction honest. By the end, you'll have a fully wired hexagonal FastAPI service you can swap, test, and extend without touching your domain. Where Does Each Piece Live? Before writing code, fix the map. A hexagonal FastAPI project has five distinct zones: app/ ├── domain/ │ ├── models.py # Money, Discount, OrderItem, Customer, Order │ └── ports.py # CustomerRepository, OrderRepository (Protocol) ├── adapters/ │ ├── sqlalchemy/ │ │ ├── orm.py # SQLAlchemy table definitions │ │ └── repositories.py # SQLAlchemyCustomerRepository, SQLAlchemyOrderRepository │ └── memory/ │ └── repositories.py # InMemoryCustomerRepo

Continue reading on Dev.to

Opens in a new tab

Read Full Article
3 views

Related Articles