← Back to all publications

Pragmatic Clean Architecture: Organizing Production Backend Services

Strip away enterprise dogma and apply Hexagonal / Clean Architecture principles to keep domain logic isolated from databases and transport frameworks.

DOMAIN FRAMEWORKS ADAPTERS USE CASES DEPENDENCY INVERSION PRINCIPLE (DIP) 1. Pure Domain Entities Zero external imports 2. Application Use Cases Business orchestration 3. Interface Adapters Controllers & repos 4. Frameworks & Drivers PostgreSQL, HTTP router ✓ Inward Dependency Rule: Inner layers never depend on outer layers Separation of Concerns: Domain Isolation from External Infrastructure Business logic runs free of framework ties • Allows painless swapping of DBs, routers, and 3rd party SDKs

The Trap of Framework Coupling

When database ORM models and HTTP controller routing annotations leak directly into business calculation logic, upgrading an HTTP framework or swapping database providers triggers massive codebase rewrites. Clean Architecture separates pure business rules from volatile external dependencies.

The Dependency Rule

Source code dependencies must point inward toward high-level domain policies:

  1. Domain Entities: Business rules, value objects, domain events (Zero framework imports).
  2. Use Cases / Application Services: Orchestrates flows and defines repository interfaces.
  3. Adapters / Infrastructure: PostgreSQL repositories, Stripe payment clients, HTTP routing handlers.
// Domain interface (inward)
export interface UserRepository {
  findById(id: string): Promise;
  save(user: User): Promise;
}

// Application use case
export class RegisterUserUseCase {
  constructor(private userRepo: UserRepository) {}

  async execute(dto: RegisterUserDto): Promise {
    const existing = await this.userRepo.findById(dto.id);
    if (existing) throw new Error('User exists');
    const user = new User(dto.id, dto.email);
    await this.userRepo.save(user);
    return user;
  }
}
HB

Written by HB

Writing on systems programming, backend architectures, and modern web engineering.

Continue Reading

Cloud Infrastructure

Architecting Distributed State at the Edge with Durable Objects

2026-09-01 • 7 min read
TypeScript

Mastering TypeScript Generics: Patterns for Robust, Type-Safe Systems

2026-09-05 • 6 min read