Overview
Contract Kit is designed to support clean architecture patterns, particularly hexagonal (ports and adapters) architecture. This separation of concerns makes your code more testable, maintainable, and adaptable.Architectural Layers
1. Domain Layer
Pure business logic with no external dependencies:2. Application Layer (Use Cases)
Orchestrates business logic and coordinates domain objects:3. Ports (Interfaces)
Define what your application needs from the outside world:4. Providers (Adapters)
Concrete implementations of ports:5. Infrastructure Layer (Contracts & Routes)
HTTP contracts and route handlers:Hexagonal Architecture
The Hexagon
Benefits
- Testability: Mock ports for unit tests
- Flexibility: Swap implementations easily
- Independence: Core logic doesn’t depend on frameworks
- Maintainability: Clear separation of concerns
Dependency Injection
Defining Dependencies
Using Dependencies
Use Cases (Commands & Queries)
Commands (Mutations)
Queries (Reads)
Testing
Unit Testing Use Cases
Integration Testing Routes
Best Practices
Keep domain logic pure
Keep domain logic pure
Domain entities and value objects should have no external dependencies.
Use dependency injection
Use dependency injection
Always access external services through ports, never directly.
Separate commands and queries
Separate commands and queries
Use different use cases for reads (queries) and writes (commands).
Keep use cases focused
Keep use cases focused
Each use case should do one thing well. Create multiple small use cases instead of large ones.
Test at the right level
Test at the right level
Unit test use cases with mocked ports. Integration test routes with real dependencies.