Intermediate LevelSoftware Engineering Principles
What is Clean Architecture (Ports and Adapters / Hexagonal Architecture)?
❓ Interview Prompt: Explain Clean Architecture, Hexagonal Architecture (Ports & Adapters), Dependency Rule, Core Domain isolation, and Infrastructure adapters.
Clean Architecture (Hexagonal / Ports & Adapters) isolates core business domain logic at the center of concentric architectural layers:
The Dependency Rule:
Dependencies MUST point strictly inward toward core domain business rules:
•
Entities & Use Cases (Center): Pure business logic (zero framework dependencies).
•
Ports (Interfaces): Input/Output boundary contracts.
•
Adapters & Drivers (Outer): Frameworks, Databases, HTTP Controllers, UI. Swapping PostgreSQL for MongoDB requires changing ONLY the outer Adapter layer!
Code Implementation Example
typescript
// 1. Port (Interface in Core Domain)
interface MessageNotifier {
send(recipient: string, body: string): Promise<void>;
}
// 2. Adapter (Infrastructure Layer implementation)
class TwilioSmsAdapter implements MessageNotifier {
async send(recipient: string, body: string) { /* Twilio SDK call */ }
}💡Implementing Hexagonal Architecture Ports and Adapters.
🎯 Interview Key Takeaway
Structure code with Clean Architecture so core business domain logic remains independent of database and web framework choices.
#Clean Architecture#Hexagonal Architecture#Ports and Adapters#Intermediate
Back to all Software Engineering Principles Questions→