Skip to content

About

A practical example of combining Hexagonal Architecture, Domain-Driven Design, and Clean Architecture principles in TypeScript. Educational codebase demonstrating a testable, maintainable architecture with clear separation of concerns.

Resources

Stars

1 star

Watchers

1 watching

Forks

Latest commit

Β 

History

3 Commits

Folders and files

Repository files navigation

πŸ”· Hexagonal Architecture + DDD + Clean Architecture Example

TypeScript Node.js Jest MIT License

This repository is a study project demonstrating the implementation of a software architecture that combines three powerful architectural approaches. It serves as a practical reference for developers who want to understand how these patterns can be effectively combined.

🎯 Purpose

This project aims to illustrate:

  • How to structure a TypeScript application using Hexagonal Architecture
  • How to implement Domain-Driven Design concepts
  • How to apply Clean Architecture principles
  • How to maintain separation of concerns and testability

This is purely educational code and is not intended for production use.

πŸ›οΈ Architectural Overview

This project combines three powerful architectural patterns:

πŸ”Ά Hexagonal Architecture (Ports & Adapters)

Hexagonal Architecture, proposed by Alistair Cockburn, organizes code to separate the business logic from external concerns by:

  • Core Domain: Contains all business logic, isolated from external dependencies
  • Ports: Interfaces that define how the application interacts with the outside world
  • Adapters: Implementations that connect external technologies to the application through ports

Hexagonal Architecture

Benefits:

  • βœ… External systems can be changed without affecting core business logic
  • βœ… Application can be driven by users, programs, tests, or batch scripts equally
  • βœ… Enables true isolation of business rules from infrastructure concerns

🧩 Domain-Driven Design (DDD)

Domain-Driven Design, introduced by Eric Evans, is an approach to software development that:

  • Focuses on the core domain logic
  • Bases complex designs on a model of the domain
  • Involves collaboration between technical and domain experts

Key DDD Concepts implemented:

  • Entities: Objects with identity (Customer)
  • Value Objects: Immutable objects without identity (Name, Address)
  • Aggregates: Clusters of entities and value objects treated as a unit
  • Repositories: Abstractions for persistence
  • Domain Services: Stateless operations that don't belong to entities
  • Ubiquitous Language: Shared language between developers and domain experts

Domain-Driven Design

πŸ§… Clean Architecture

Clean Architecture, proposed by Robert C. Martin (Uncle Bob), emphasizes:

  • Independence of frameworks
  • Testability independent of UI, database, etc.
  • Independence from any external agency

The architectural pattern consists of concentric layers:

  • Entities: Enterprise business rules
  • Use Cases: Application-specific business rules
  • Interface Adapters: Adapters between use cases and external systems
  • Frameworks & Drivers: External frameworks and tools

Clean Architecture

Key principles:

  • βœ… Dependency Rule: Source code dependencies point only inward
  • βœ… Screaming Architecture: Architecture should scream the intended use
  • βœ… Clean Boundaries: Clear boundaries between layers

πŸ“‚ Project Structure

src/
β”œβ”€β”€ application/         # Application layer (use cases)
β”‚   └── customer/
β”‚       β”œβ”€β”€ ports/       # Input ports (interfaces for use cases)
β”‚       β”‚   └── in/      # Input interfaces used by external systems
β”‚       └── use_cases/   # Use case implementations
β”‚
β”œβ”€β”€ domain/              # Domain layer (application core)
β”‚   └── customer/
β”‚       β”œβ”€β”€ entities/    # Domain entities (Customer)
β”‚       β”œβ”€β”€ enums/       # Domain enumerations (CustomerStatus)
β”‚       β”œβ”€β”€ exceptions/  # Domain-specific exceptions
β”‚       β”œβ”€β”€ repositories/# Output ports (repository interfaces)
β”‚       └── value_objects/# Value objects (Name, Address, ZipCode)
β”‚
β”œβ”€β”€ infrastructure/      # Infrastructure layer (adapters)
β”‚   └── persistence/
β”‚       └── memory/      # In-memory repository implementations
β”‚
└── config/              # Application configurations (DI, etc.)

tests/
β”œβ”€β”€ unit/               # Unit tests
β”‚   β”œβ”€β”€ domain/         # Domain tests
β”‚   β”œβ”€β”€ application/    # Application layer tests
β”‚   └── infrastructure/ # Infrastructure tests
β”‚
└── integration/        # Integration tests

🧩 Project Components

Domain Layer

The domain layer contains the business logic and rules of the application:

  • Entities: Customer - Core business objects with identity and lifecycle
  • Value Objects: Name, Address, ZipCode - Immutable objects that describe characteristics
  • Repositories Interfaces: CustomerRepositoryPort - Ports for data persistence
  • Enums: CustomerStatus - Domain-specific enumerations
  • Exceptions: Domain-specific exceptions for validation and business rules

Application Layer

The application layer orchestrates the flow of data to and from the domain:

  • Use Cases: CreateCustomerService, GetCustomersService - Application-specific business rules
  • Ports: CreateCustomerUseCase, GetCustomersUseCase - Interfaces for use cases

Infrastructure Layer

The infrastructure layer provides implementations to external systems:

  • Repositories: CustomerRepositoryMemory - In-memory implementation of repository interfaces
  • Configuration: DependencyInjection - Wires up the application components

πŸ“ Architectural Principles Applied

Dependency Inversion Principle

  • Domain entities don't depend on application services
  • Application services don't depend on infrastructure implementations
  • Dependencies point inward toward the domain

Separation of Concerns

  • Each layer has a clear and distinct responsibility
  • The domain doesn't know about persistence
  • The application layer doesn't know about HTTP, CLI, etc.

Testability

  • Every component can be tested in isolation
  • The domain can be tested without infrastructure
  • Use cases can be tested with mocked repositories

πŸ’» How to Run

# Install dependencies
npm install

# Run the application
npm run dev

# Run tests
npm test

# Run only unit tests
npm run test:unit

# Run only integration tests
npm run test:integration

# Build the project
npm run build

# Run the compiled version
npm start

πŸ§ͺ Testing Strategy

This project uses a comprehensive testing approach:

  • Unit Tests: Test individual components in isolation

    • Domain entities and value objects
    • Use cases with mocked dependencies
    • Repository implementations
  • Integration Tests: Test how components work together

    • Flow between use cases and repositories
    • End-to-end business operations

⚠️ Limitations

This project is intentionally simple to facilitate understanding of architectural concepts. In a real application, you would need to consider:

  • Persistence in a real database
  • More robust error handling
  • More comprehensive validations
  • Authentication and authorization
  • Logging and monitoring
  • Event-driven communication between bounded contexts
  • CQRS (Command Query Responsibility Segregation)
  • Event Sourcing
  • Performance considerations

πŸ“š Further Reading

πŸ“„ License

This project is made available as open source under the MIT license.

About

A practical example of combining Hexagonal Architecture, Domain-Driven Design, and Clean Architecture principles in TypeScript. Educational codebase demonstrating a testable, maintainable architecture with clear separation of concerns.

Resources

Stars

1 star

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages