# DDD Implementation Skill

A Claude Code skill that acts as a Senior DDD Developer, implementing domain-driven code following Clean Architecture principles.

## What This Skill Does

This skill helps you implement DDD code that follows established patterns and rules:

✅ **Aggregates** - With proper invariant enforcement and Result types
✅ **Entities** - Child entities within aggregates
✅ **Value Objects** - Immutable, self-validating
✅ **Use Cases** - In application layer with proper error handling
✅ **Repositories** - Interfaces in domain, implementations in infrastructure
✅ **Domain Events** - For cross-aggregate communication

## When to Use This Skill

**Use `/ddd-implement` when**:
- You have a domain model designed (from `/ddd-model`)
- You need to implement specific aggregates, entities, or value objects
- You want code that follows DDD rules automatically
- You need proper error handling (Result types for Java, errors for Go)
- You want layer boundaries respected

**Don't use this skill for**:
- Domain modeling and design (use `/ddd-model` instead)
- Generic coding tasks (use default Claude Code)
- Non-DDD projects

## Usage Examples

### Implement an Aggregate

```bash
/ddd-implement --lang=java "Implement Order aggregate with addLineItem, removeLineItem, and cancel methods"
```

The skill will:
1. Create the aggregate in `domain/order/`
2. Add proper invariants (e.g., "Cannot modify cancelled order")
3. Use Result types for error handling
4. Implement sealed interfaces for errors
5. Add domain events (OrderCreated, OrderCancelled, etc.)

### Implement a Use Case

```bash
/ddd-implement --lang=go "Implement PlaceOrder use case that creates an order and reserves inventory"
```

The skill will:
1. Create use case in `application/order/`
2. Use repository interfaces (not implementations)
3. Handle errors properly
4. Return DTOs (not domain objects)
5. Add transaction boundaries if needed

### Implement a Repository

```bash
/ddd-implement --lang=java "Implement PostgreSQL repository for Order aggregate"
```

The skill will:
1. Create implementation in `infrastructure/order/persistence/`
2. Implement the domain interface
3. Add exception boundary (catch SQL exceptions → return domain errors)
4. Map between domain model and database schema

### Implement from Existing File

```bash
/ddd-implement internal/domain/account/account.go
```

The skill will:
1. Detect language from file extension
2. Read existing code
3. Suggest improvements or complete partial implementations
4. Follow established patterns in the file

## What Makes This Skill Different

### Enforces DDD Rules Automatically

The skill knows and enforces:
- Aggregate boundaries (no direct aggregate-to-aggregate references)
- Invariant documentation and enforcement
- Entity equality (ID-based only)
- Value Object immutability
- Repository patterns (interface in domain, impl in infrastructure)
- Layer dependencies (domain has no external deps)

### Language-Aware

**For Java**:
- Uses Result<E, T> types (no exceptions from domain/application)
- Uses sealed interfaces for errors
- Uses pattern matching with switch expressions
- Uses static imports for Failure/Success
- Follows Java 21+ conventions

**For Go**:
- Uses error return values
- Uses pointer receivers for aggregates/entities
- Uses value receivers for value objects
- Uses sentinel errors and custom error types
- Follows Uber Go Style Guide

### Error Handling Built-In

The skill automatically:
- Returns Result types (Java) or errors (Go)
- Creates layer-specific error types
- Adds exception boundaries at infrastructure layer
- Logs errors appropriately (ERROR/WARN/INFO levels)
- Prevents silent failures

## How It Works

1. **Analyzes your request** - Determines what to implement (aggregate, use case, etc.)
2. **Detects language** - From flags, file extensions, or project structure
3. **Loads rules** - DDD rules, error handling, style guides for your language
4. **Generates code** - Following templates and patterns
5. **Validates** - Checks against DDD rules before completion

## Architecture

```
/ddd-implement
├── SKILL.md              # Skill manifest (loaded by Claude Code)
├── system-prompt.md      # Core instructions for the implementation agent
├── README.md             # This file
└── examples/
    ├── go-example.md     # Example session in Go
    └── java-example.md   # Example session in Java
```

The `system-prompt.md` references rules from the `ddd-model` skill:
- `ddd-model/rules/ddd-rules.md` - Core DDD patterns
- `ddd-model/rules/error-handling.md` - Error handling strategy
- `ddd-model/languages/{lang}/style-guide.md` - Language conventions
- `ddd-model/languages/{lang}/templates/` - Code templates

## Workflow: Modeling → Implementation

```
1. Design with /ddd-model
   ↓ (identifies aggregates, entities, invariants)

2. Implement with /ddd-implement
   ↓ (generates code following rules)

3. Review with /review (or code review)
   ↓ (validates DDD principles)

4. Iterate
```

## Examples

See detailed examples in:
- [Java Example Session](./examples/java-example.md)
- [Go Example Session](./examples/go-example.md)

## Rules Reference

This skill enforces rules from:

- **DDD Rules**: [ddd-model/rules/ddd-rules.md](../ddd-model/rules/ddd-rules.md)
- **Clean Architecture**: [ddd-model/rules/clean-arch.md](../ddd-model/rules/clean-arch.md)
- **Error Handling**: [ddd-model/rules/error-handling.md](../ddd-model/rules/error-handling.md)
- **Java Style**: [ddd-model/languages/java/style-guide.md](../ddd-model/languages/java/style-guide.md)
- **Go Style**: [ddd-model/languages/go/style-guide.md](../ddd-model/languages/go/style-guide.md)

## Contributing

To improve this skill:

1. **Add examples** - Real-world implementation sessions in `examples/`
2. **Refine rules** - Update `system-prompt.md` based on experience
3. **Add templates** - Language-specific templates in `ddd-model/languages/{lang}/templates/`
4. **Document patterns** - Special patterns in `ddd-model/rules/`

## Related Skills

- **ddd-model** - For domain modeling and design
- **review** - For code review with DDD principles (if available)
