This is the heart of DDD — the Domain Layer, representing the core business logic and rules.
Contains entities like User.java.
Justification: DDD emphasizes rich domain models. Entities encapsulate data and behavior, representing the ubiquitous language of the business.
Contains BusinessRules.java.
Justification: Domain Services handle domain logic that doesn’t naturally belong to a single entity. They implement complex business rules directly in the domain layer.
Contains DomainEvent.java, UserCreatedEvent.java, EventPublisher.java.
Justification: DDD encourages capturing domain events to reflect important business occurrences. Events decouple the domain from infrastructure and allow eventual consistency or integration with other bounded contexts.
This is the Application Layer in DDD — orchestrates domain objects to fulfill use cases, without embedding business logic.
Contains interfaces like CreateUserUseCase.java, GetUserUseCase.java.
Justification: Defines what the application can do (use cases) without exposing implementation details. This aligns with DDD’s separation of application logic from domain logic.
Contains interfaces like UserRepositoryPort.java, EventPublisherPort.java.
Justification: Outgoing ports abstract dependencies on external systems (DB, messaging). This allows domain logic to remain independent from frameworks and infrastructure.
Contains CreateUserService.java, GetUserService.java, EventDrivenService.java.
Justification: Implements application use cases by orchestrating domain objects and invoking domain services/events. Keeps orchestration separate from domain rules.
This is the Adapters Layer (or Infrastructure/Interface adapters) — bridges domain/application to external systems.
Contains UserController.java, HealthCheckController.java.
Justification: Incoming adapters that convert HTTP requests into calls to application ports. Controllers never contain business rules, preserving DDD principles.
Contains JpaUserRepositoryAdapter.java, SpringDataUserRepository.java.
Justification: Outgoing adapters implementing UserRepositoryPort. They persist domain entities but do not embed domain logic.
Contains LambdaHandler.java.
Justification: Another incoming adapter for AWS Lambda, transforming events into calls to application use cases.
Contains KafkaEventPublisherAdapter.java, SnsEventPublisherAdapter.java, SqsEventListener.java, EventDeserializer.java.
Justification: Handles domain events asynchronously. Implements ports for publishing/consuming events, keeping domain unaware of messaging frameworks.
Contains BeanConfig.java, KafkaConfig.java, SnsConfig.java, SqsConfig.java.
Justification: Infrastructure configuration. Keeps domain pure, avoiding framework-specific code inside domain/application layers.
Contains LoggingAspect.java, TracingInterceptor.java.
Justification: Cross-cutting concerns for monitoring and tracing. DDD domains remain independent of these technical concerns.
Contains GlobalExceptionHandler.java.
Justification: Centralized handling of errors. Domain exceptions can propagate through application/adapters, keeping domain logic clean.
Contains MetricsCollector.java.
Justification: Tracks performance and events, outside domain logic.
Justification: Bootstraps the application but does not contain domain logic. All core business rules remain in domain/.
| Folder | DDD Role |
|---|---|
domain/model | Core entities (ubiquitous language) |
domain/service | Domain services implementing business rules |
domain/event | Captures business events, decouples domain from infrastructure |
application/port/input | Defines use cases, separates orchestration from domain |
application/port/output | Abstracts external systems for domain independence |
application/service | Orchestrates domain entities and services per use case |
adapter/rest | Incoming adapter (HTTP → application ports) |
adapter/persistence | Outgoing adapter (application ports → DB) |
adapter/lambda | Incoming adapter (Lambda → application ports) |
adapter/events | Handles asynchronous events, decouples domain from messaging |
adapter/config | Technical configurations, keeps domain pure |
infrastructure | Cross-cutting concerns (logging, metrics, exceptions) |
Why this folder structure perfectly supports DDD:
domain/.