How dev-lusaja/fastapi-hexagonal works
Main Components
• Domain Layer:
– Contains the business core with entities (app/domain/entities), events (app/domain/events), repositories interfaces (app/domain/repositories), and use cases (app/domain/use_cases) along with domain-specific exceptions (app/domain/exceptions.py).
• Application Layer:
– Responsible for orchestrating use cases and validations. It includes service implementations (app/application/services) and input data validations (app/application/validators).
• Infrastructure Layer:
– Provides concrete implementations and external integrations. This includes API handlers (app/infrastructure/handlers), repository implementations (app/infrastructure/repositories), event adapters (app/infrastructure/events), data schemas (app/infrastructure/schemas), and the dependency injection container (app/infrastructure/container.py).
• Test Layer:
– Contains tests for services, API endpoints, and test data (under app/test).
Relationships and Data Flow
• Domain is at the center and defines all core business logic and interfaces (or “ports”).
• The Application layer sits next to the Domain and implements the use cases by calling into domain logic, leveraging input validators.
• The Infrastructure layer encircles both Domain and Application as “adapters” that provide actual I/O operations (e.g., web APIs via FastAPI in app/infrastructure/fast_api.py, repository implementations, etc.).
• The Test layer interacts with all layers by verifying that the behavior adheres to expectations.
Architectural Patterns and Design Principles
• Visually distinguish the Domain (the inner hexagon/core) from the Application layer (which implements business use cases) and the outer Infrastructure layer (adapters performing I/O).
• That Domain should be independent – it contains no third-party libraries, while Infrastructure integrates with frameworks like FastAPI, dependency-injector, and pydantic.
• Dependency inversion where the Application and Infrastructure layers depend on Domain interfaces rather than the other way around.
Relevant Technologies
• Python is the main language.
• FastAPI serves as the web framework (located in app/infrastructure/fast_api.py and app/infrastructure/handlers).
• dependency-injector is used for dependency management (app/infrastructure/container.py).
• pytest is used for testing (test files under app/test).
• Docker and Make are used for containerization and build orchestration.