$npx -y skills add giuseppe-trisciuoglio/developer-kit --skill clean-architectureProvides implementation patterns for Clean Architecture, Hexagonal Architecture (Ports & Adapters), and Domain-Driven Design in Java 21+ Spring Boot 3.5+ applications. Use when structuring layered architectures, separating domain logic from frameworks, implementing ports and adap
| 1 | # Clean Architecture, Hexagonal Architecture & DDD for Spring Boot |
| 2 | |
| 3 | ## Overview |
| 4 | |
| 5 | This skill provides comprehensive guidance for implementing Clean Architecture, Hexagonal Architecture (Ports & Adapters), and Domain-Driven Design tactical patterns in Java 21+ Spring Boot 3.5+ applications. It ensures clear separation of concerns, framework-independent domain logic, and highly testable codebases through proper layering and dependency management. |
| 6 | |
| 7 | ## When to Use |
| 8 | |
| 9 | - Architecting new Spring Boot applications with clear separation of concerns |
| 10 | - Refactoring tightly coupled code into testable, layered architectures |
| 11 | - Implementing domain logic independent of frameworks and infrastructure |
| 12 | - Designing ports and adapters for swappable implementations |
| 13 | - Applying Domain-Driven Design tactical patterns (entities, value objects, aggregates) |
| 14 | - Creating testable business logic without Spring context dependencies |
| 15 | |
| 16 | ## Instructions |
| 17 | |
| 18 | ### 1. Understand the Core Concepts |
| 19 | |
| 20 | #### Clean Architecture Layers (Dependency Rule) |
| 21 | |
| 22 | Dependencies flow inward. Inner layers know nothing about outer layers. |
| 23 | |
| 24 | | Layer | Responsibility | Spring Boot Equivalent | |
| 25 | |-------|---------------|----------------------| |
| 26 | | **Domain** | Entities, value objects, domain events, repository interfaces | `domain/` - no Spring annotations | |
| 27 | | **Application** | Use cases, application services, DTOs, ports | `application/` - `@`Service, `@`Transactional | |
| 28 | | **Infrastructure** | Frameworks, database, external APIs | `infrastructure/` - `@`Repository, `@`Entity | |
| 29 | | **Adapter** | Controllers, presenters, external gateways | `adapter/` - `@`RestController | |
| 30 | |
| 31 | #### Hexagonal Architecture (Ports & Adapters) |
| 32 | |
| 33 | - **Domain Core**: Pure Java business logic, no framework dependencies |
| 34 | - **Ports**: Interfaces defining contracts (driven and driving) |
| 35 | - **Adapters**: Concrete implementations (JPA, REST, messaging) |
| 36 | |
| 37 | #### Domain-Driven Design Tactical Patterns |
| 38 | |
| 39 | - **Entities**: Objects with identity and lifecycle (e.g., `Order`, `Customer`) |
| 40 | - **Value Objects**: Immutable, defined by attributes (e.g., `Money`, `Email`) |
| 41 | - **Aggregates**: Consistency boundary with root entity |
| 42 | - **Domain Events**: Capture significant business occurrences |
| 43 | - **Repositories**: Persistence abstraction, implemented in infrastructure |
| 44 | |
| 45 | ### 2. Organize Package Structure |
| 46 | |
| 47 | Follow this feature-based package organization: |
| 48 | |
| 49 | ``` |
| 50 | com.example.order/ |
| 51 | ├── domain/ |
| 52 | │ ├── model/ # Entities, value objects |
| 53 | │ ├── event/ # Domain events |
| 54 | │ ├── repository/ # Repository interfaces (ports) |
| 55 | │ └── exception/ # Domain exceptions |
| 56 | ├── application/ |
| 57 | │ ├── port/in/ # Driving ports (use case interfaces) |
| 58 | │ ├── port/out/ # Driven ports (external service interfaces) |
| 59 | │ ├── service/ # Application services |
| 60 | │ └── dto/ # Request/response DTOs |
| 61 | ├── infrastructure/ |
| 62 | │ ├── persistence/ # JPA entities, repository adapters |
| 63 | │ └── external/ # External service adapters |
| 64 | └── adapter/ |
| 65 | └── rest/ # REST controllers |
| 66 | ``` |
| 67 | |
| 68 | ### 3. Implement the Domain Layer (Framework-Free) |
| 69 | |
| 70 | The domain layer must have zero dependencies on Spring or any framework. |
| 71 | |
| 72 | - Use Java records for immutable value objects with built-in validation |
| 73 | - Place business logic in entities, not services (Rich Domain Model) |
| 74 | - Define repository interfaces (ports) in the domain layer |
| 75 | - Use strongly-typed IDs to prevent ID confusion |
| 76 | - Implement domain events for decoupling side effects |
| 77 | - Use factory methods for entity creation to enforce invariants |
| 78 | |
| 79 | ### 4. Implement the Application Layer |
| 80 | |
| 81 | - Create use case interfaces (driving ports) in `application/port/in/` |
| 82 | - Create external service interfaces (driven ports) in `application/port/out/` |
| 83 | - Implement application services with `@Service` and `@Transactional` |
| 84 | - Use DTOs for request/response, separate from domain models |
| 85 | - Publish domain events after successful operations |
| 86 | |
| 87 | ### 5. Implement the Infrastructure Layer (Adapters) |
| 88 | |
| 89 | - Create JPA entities in `infrastructure/persistence/` |
| 90 | - Implement repository adapters that map between domain and JPA entities |
| 91 | - Use MapStruct or manual mappers for domain-JPA conversion |
| 92 | - Configure conditional beans for swappable implementations |
| 93 | - Keep infrastructure concerns isolated from domain logic |
| 94 | |
| 95 | ### 6. Implement the Adapter Layer (REST) |
| 96 | |
| 97 | - Create REST controllers in `adapter/rest/` |
| 98 | - Inject use case interfaces, not implementations |
| 99 | - Use Bean Validation on DTOs |
| 100 | - Return proper HTTP status codes and responses |