Architecture Intermediate
Hexagonal Architecture — Ports and Adapters¶
HexagonalPorts & AdaptersClean Code 3 min read
Separating business logic from infrastructure. Ports, adapters, and testable code.
Principle¶
Business logic does not depend on the database, framework, or UI. Ports = interfaces. Adapters = concrete implementations.
Structure¶
src/
├── domain/ # Core — no dependencies!
│ ├── model/Order.ts
│ ├── ports/OrderRepository.ts # Interface
│ └── services/OrderService.ts
├── application/
│ └── CreateOrderUseCase.ts
└── infrastructure/ # Adapters
├── persistence/PostgresOrderRepo.ts
├── payment/StripeGateway.ts
└── web/OrderController.ts
Testability¶
Domain logic is tested with mock adapters — no database, no HTTP.
const mockRepo = { save: jest.fn() };
const service = new OrderService(mockRepo, mockPayment);
test('creates order', async () => {
const order = await service.createOrder('cust-1', items);
expect(order.status).toBe('CREATED');
expect(mockRepo.save).toHaveBeenCalled();
});
Summary¶
Hexagonal architecture is an investment in maintainability. It pays off for projects with a longer lifespan.
Need Help with Implementation?¶
Our team has experience designing and implementing modern architectures. We’re happy to help.