|
| 1 | +# Strengthen Domain Invariant Enforcement (DDD Refactor) |
| 2 | + |
| 3 | +**Issue**: [#281](https://github.com/torrust/torrust-tracker-deployer/issues/281) |
| 4 | +**Type**: Refactor |
| 5 | +**Status**: In Progress |
| 6 | + |
| 7 | +## Overview |
| 8 | + |
| 9 | +Apply the DDD validated constructor pattern to all domain configuration types, ensuring domain invariants are enforced at construction time rather than validated post-hoc. |
| 10 | + |
| 11 | +## Detailed Plan |
| 12 | + |
| 13 | +See: [`docs/refactors/plans/strengthen-domain-invariant-enforcement.md`](../refactors/plans/strengthen-domain-invariant-enforcement.md) |
| 14 | + |
| 15 | +## Related ADRs |
| 16 | + |
| 17 | +- [Validated Deserialization for Domain Types](../decisions/validated-deserialization-for-domain-types.md) |
| 18 | +- [TryFrom for DTO to Domain Conversion](../decisions/tryfrom-for-dto-to-domain-conversion.md) |
| 19 | + |
| 20 | +## Reference Implementation |
| 21 | + |
| 22 | +`HttpApiConfig` has been refactored as the reference implementation (Phase 0, Proposal #0): |
| 23 | + |
| 24 | +- Location: `src/domain/tracker/config/http_api.rs` |
| 25 | +- DTO: `src/application/command_handlers/create/config/tracker/http_api_section.rs` |
| 26 | + |
| 27 | +## Implementation Checklist |
| 28 | + |
| 29 | +### Phase 0: HTTP API Config ✅ |
| 30 | + |
| 31 | +- [x] `HttpApiConfig` validated constructor with `HttpApiConfigError` |
| 32 | +- [x] Private fields with getter methods |
| 33 | +- [x] Custom `Deserialize` using `HttpApiConfigRaw` |
| 34 | +- [x] `TryFrom<HttpApiSection> for HttpApiConfig` |
| 35 | +- [x] Documentation and ADRs |
| 36 | + |
| 37 | +### Phase 1: Tracker Configuration Types |
| 38 | + |
| 39 | +- [ ] `UdpTrackerConfig` - validated constructor, private fields, getters |
| 40 | +- [ ] `HttpTrackerConfig` - validated constructor, private fields, getters |
| 41 | +- [ ] `HealthCheckApiConfig` - validated constructor, private fields, getters |
| 42 | +- [ ] `TryFrom` implementations for each DTO section |
| 43 | + |
| 44 | +### Phase 2: Cross-Cutting Invariants |
| 45 | + |
| 46 | +- [ ] `TrackerCoreConfig` - database configuration validation |
| 47 | +- [ ] `TrackerConfig` - validates at construction (socket conflicts) |
| 48 | +- [ ] `UserInputs` - validated constructor (Grafana requires Prometheus) |
| 49 | + |
| 50 | +## Acceptance Criteria |
| 51 | + |
| 52 | +- [ ] All domain configuration types use validated constructors |
| 53 | +- [ ] All fields are private with getter methods |
| 54 | +- [ ] All types implement custom `Deserialize` with validation |
| 55 | +- [ ] All DTO→Domain conversions use `TryFrom` trait |
| 56 | +- [ ] Validation logic moved from application to domain layer |
| 57 | +- [ ] Pre-commit checks pass: `./scripts/pre-commit.sh` |
| 58 | + |
| 59 | +## Contributing Guide |
| 60 | + |
| 61 | +See [`docs/contributing/ddd-practices.md`](../contributing/ddd-practices.md) for implementation patterns. |
0 commit comments