Skip to content

[Refactor] Phase 4: Service Topology DDD Layer Alignment #301

Description

@josecelano

Overview

Move port derivation and network computation logic from the infrastructure layer to the domain layer, ensuring proper DDD layer separation. This phase was identified during Phase 3 implementation when we discovered business rules incorrectly placed in infrastructure.

Problem Statement

The current architecture has domain logic (port derivation, network computation) incorrectly placed in the infrastructure layer:

infrastructure/context/port_derivation.rs  ← Business rules about port exposure
infrastructure/context/tracker.rs          ← compute_networks() method
infrastructure/context/grafana.rs          ← compute_networks() method
infrastructure/context/prometheus.rs       ← compute_networks() method

These are business rules that should be in the domain layer:

  • "UDP ports are always exposed (no TLS for UDP)" - PORT-02
  • "HTTP ports hidden when TLS enabled" - PORT-03, PORT-04
  • "Tracker joins metrics_network when Prometheus is enabled"

Implementation Plan

P4.1: Add Trait and Implement in Domain

  • Create src/domain/topology/traits.rs with PortDerivation trait
  • Implement PortDerivation for TrackerConfig, GrafanaConfig, PrometheusConfig
  • Create src/domain/topology/fixed_ports.rs for Caddy and MySQL
  • Add unit tests for each implementation

P4.2: Create Domain Topology Builder

  • Create src/domain/topology/builder.rs with DockerComposeTopologyBuilder
  • Move network computation logic from infrastructure to domain builder
  • Wire up port derivation via trait calls
  • Add integration tests

P4.3: Refactor Infrastructure to Pure DTOs

  • Remove compute_networks() from all service context types
  • Rename types to *Context (e.g., TrackerServiceContext)
  • Update DockerComposeContextBuilder to receive domain topology
  • Update tests

P4.4: Cleanup

  • Delete src/infrastructure/.../context/port_derivation.rs
  • Remove any remaining business logic from infrastructure
  • Update documentation
  • Run full E2E test suite

Acceptance Criteria

  • Pre-commit checks pass: ./scripts/pre-commit.sh
  • PortDerivation trait defined in domain/topology/traits.rs
  • All service configs implement PortDerivation
  • DockerComposeTopologyBuilder computes networks in domain layer
  • Infrastructure context types are pure DTOs with no compute_*() methods
  • port_derivation.rs deleted from infrastructure
  • All existing E2E tests pass

Related

Metadata

Metadata

Assignees

Type

No type

Fields

No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions