DDD Standard Architecture Rules
1. Overall Architecture and Layer Definitions
- The system must be clearly divided into Domain、Application、Infrastructure、Presentation four layers.
- Each file may belong to only one layer and must not simultaneously bear responsibilities of multiple layers.
- The purpose of layering is to isolate "business intent" from "technical details," and layers must not be merged for convenience.
- Architectural design must aim at long-term evolution and minimizing replacement cost.
2. Dependency Direction (Must Not Be Violated)
- Only the following unidirectional dependencies are allowed:
Domain → Application → Infrastructure → Presentation
- Any reverse dependency is to be regarded as an architectural error.
- Reverse dependencies must not be formed indirectly through types, utility functions, barrel exports, or side effects.
- Dependency direction must be statically analyzable at the TypeScript
import level.
3. Domain Layer Rules (Business Core)
- The Domain layer must be pure TypeScript.
- The Domain layer must not depend on Angular, RxJS, Signals, HTTP, Storage, or any framework.
- The Domain layer must not have
async / await、Promise or I/O behavior.
- The Domain layer only describes "what the business is," not "how it is done."
- The Domain layer may contain only:
- Entity
- Value Object
- Domain Service
- Domain Interface
- An Entity must have a clear identity.
- A Value Object must be immutable.
- A Domain Service may express only business rules that cannot naturally belong to a single Entity.
- A Domain Interface only describes capabilities, not implementation methods.
- The Domain layer must not know where data comes from, how it is stored, or how it is displayed.
4. Application Layer Rules (Business Processes and State Coordination)
- The Application layer is responsible for "Use Cases" and business process orchestration.
- The Application layer is the sole holder of the truth of business state for the entire system.
- The Application layer may depend on the Domain, but must not reversely depend on Presentation or Infrastructure implementations.
- The Application layer may depend only on interfaces defined by Infrastructure defined interface, not concrete classes.
- The Application layer is responsible for transaction boundaries and process order.
- The Application layer must not contain concepts related to UI, DOM, Component, or Template.
- The Application layer must not contain data access details.
- The Application layer must not leak framework types to other layers.
- Application Facade is the only entry point that Presentation is allowed to access.
5. Infrastructure Layer Rules (Technical Implementation)
- The Infrastructure layer is responsible only for implementing interfaces defined by the Domain or Application.
- The Infrastructure layer may depend on frameworks and third-party packages.
- The Infrastructure layer must not contain business rules or decision logic.
- The Infrastructure layer must not itself become the source of truth for state.
- The Infrastructure layer must not require upper layers (Application / Domain) to adjust their design to accommodate technical details.
- The Infrastructure layer must not expose framework-specific types externally.
- The Infrastructure layer can be replaced without affecting the Domain and Application.
6. Presentation Layer Rules (UI and Interaction)
- The Presentation layer is responsible only for display and user interaction.
- The Presentation layer must not contain business rules.
- The Presentation layer must not define business state truth on its own.
- The Presentation layer may only depend on the Application Facade.
- The Presentation layer must not directly call Infrastructure.
- The Presentation layer must not bypass Application to directly operate on Domain.
- The Presentation layer's state may only be short-lived UI state.
VII. State and Responsibility Boundaries
- The Domain layer does not hold application state.
- The Application layer centrally manages all business state.
- The Infrastructure layer must not hold long-lived business state.
- The Presentation layer must not hold business state that spans pages or flows.
- Any state may only have a single source of authority (Single Source of Truth).
VIII. Interface and Abstraction Rules
- The definition location of an Interface must belong to the requester, not the implementer.
- For capabilities that Application needs, their interface must be defined in Application or Domain.
- Infrastructure may only implement interfaces and must not define requirements in reverse.
- Interfaces must not leak technical details.
IX. Shared and Common Module Rules
shared must not become a business core.
shared must not contain business state or decisions.
- If
shared is regarded by multiple features as a business dependency, its layer must be moved up to Application or Domain.
shared may only contain:
- Pure utilities
- Pure UI
- Pure stateless components
X. Structural and Naming Consistency
- The folder structure must reflect layering and responsibility.
- File names and folder names must allow direct inference of the layer to which they belong.
- Naming whose semantics do not match actual responsibility must not appear.
- Barrel exports must not blur layer boundaries.
XI. Validation and Evolution Rules
- Domain must be able to compile and be tested independently of any framework.
- Application must be able to run and be tested without a UI.
- Infrastructure must be mockable or replaceable.
- Replacing Presentation must not affect business logic.
- Architecture rules must be checkable through lint, test, or CI.