Skip to main content

Aggregate design canvas

Synced from product-master

This page is a copy of docs/docs/ddd/aggregate-design-canvas.md on develop, derived from that repository's code. Edit it there, then re-sync.

Following the ddd-crew Aggregate Design Canvas v1.1. There is one aggregate root in internal/domain: Product.

Product​

1. Name​

Product (internal/domain/product/product.go).

2. Description​

The master record of one SKU: an optional description, an optional handling Classification with its ClassificationSource, and a PhysicalProfile (declared dimensions plus the latest measurement). Identity is the SKU. Every accepted change increments version, which guards persistence and rides on every published event. The aggregate does no I/O; the caller passes now.

3. State Transitions​

The aggregate has no status enum. Its observable states are whether it is classified (and by which source) and which parts of the physical profile are set; version increases on every transition drawn below except the self-loops marked "no change".

Source: internal/domain/product/product.go (Register, ChangeDescription, Classify, setClassification, DeclareDimensions, RecordMeasurement), internal/domain/product/physical.go. ClassifiedLegacy is reachable only through Rehydrate: a row whose classification_source is legacy-import was written by the ADR 0003 importer before stage E (2026-10-17) removed it, and no command sets that source any more (SourceLegacyImport in internal/domain/product/classification.go stays a valid stored value). Omits: Rehydrate (rebuilds a stored product without events) and the absence of any delete, unclassify or unregister operation in v1. Identical commands are no change in every state (no event, no version bump).

4. Enforced Invariants​

InvariantEnforced by
SKU is 1..64 characters, no whitespace, control characters or /; never changesNewSKU (ErrInvalidSKU); no setter
Description at most 200 characters, no control charactersvalidateDescription (ErrInvalidDescription)
Classification: at least one tag, closed set, no duplicatesNewClassification (ErrNoHandlingTags, ErrUnknownHandlingTag, ErrDuplicateHandlingTag)
TemperatureClass iff TemperatureSensitive, value in the closed setcheckTemperature (ErrTemperatureClassRequired, ErrTemperatureClassNotApplicable, ErrUnknownTemperatureClass)
DOTHazardClass 1..9 and only with Hazmat (0 = not recorded)checkDOT (ErrInvalidDOTHazardClass, ErrDOTHazardClassNotApplicable); also CHECK (dot_hazard_class BETWEEN 1 AND 9)
A classification always has a source, and vice versasetClassification; CHECK ((handling_tags IS NULL) = (classification_source IS NULL))
Dimensions 1..20000 mm, weight 1..2000000 gNewUnitDimensions (ErrInvalidDimension, ErrInvalidWeight)
A measurement has measuredAt, not after the clock; device id at most 64 runes, no control charactersNewMeasurement (ErrMissingMeasuredAt, ErrMeasuredAtInFuture, ErrInvalidDeviceID)
The latest measurement is never replaced by an older oneRecordMeasurement (ErrStaleMeasurement)
version starts at 1, +1 per accepted change, unchanged by a no-opRegister, bump; CHECK (version >= 1); the version-guarded Save

5. Corrective Policies​

  • A REST caller gets a 400 with the slug of the broken rule, 404 product-not-found for an unregistered SKU, 409 stale-measurement for an older reading, or 409 concurrent-modification when another writer saved a newer version first; it must re-fetch and retry (internal/adapters/inbound/http/errors.go).
  • A discrepancy above 10 % is not corrected automatically: the measurement is recorded and becomes effective, and the flag is published for stewards (ADR 0002).

6. Handled Commands​

CommandUse caseInbound
RegisterProductCommandRegisterProduct.Handle (Register or ChangeDescription)PUT /products/{sku}
ClassifyProductCommandClassifyProduct.Handle (Classify)PUT /products/{sku}/classification
DeclareDimensionsCommandDeclareDimensions.Handle (DeclareDimensions)PUT /products/{sku}/dimensions/declared
RecordMeasurementCommandRecordMeasurement.Handle (RecordMeasurement)PUT /products/{sku}/dimensions/measured
DeclarePackLevelCommandDeclarePackLevel.Handle (DeclarePackLevel)PUT /products/{sku}/pack-levels/{level}, MCP declare_pack_level
RetirePackLevelCommandRetirePackLevel.Handle (RetirePackLevel)POST /products/{sku}/pack-levels/{level}/retirements, MCP retire_pack_level
RecordPackLevelMeasurementCommandRecordPackLevelMeasurement.Handle (RecordPackLevelMeasurement)PUT /products/{sku}/pack-levels/{level}/measured

7. Created Events​

EventWhenFull CloudEvents type
ProductRegisteredRegistercom.warehouse.wms.product-master.product.ProductRegistered
ProductDescriptionChangedChangeDescription with different textcom.warehouse.wms.product-master.product.ProductDescriptionChanged
ProductClassifiedClassify that changes the classification or its source (confirming a legacy-import row makes it native)com.warehouse.wms.product-master.product.ProductClassified
ProductDimensionsDeclaredDeclareDimensions with different valuescom.warehouse.wms.product-master.product.ProductDimensionsDeclared
ProductMeasuredRecordMeasurement with a new readingcom.warehouse.wms.product-master.product.ProductMeasured
ProductPackLevelDeclaredDeclarePackLevel that declares a level or changes its flags or declared dimensionscom.warehouse.wms.product-master.product.ProductPackLevelDeclared
ProductPackLevelRetiredRetirePackLevel, or DeclarePackLevel with a new GTIN at an active level (reason superseded)com.warehouse.wms.product-master.product.ProductPackLevelRetired
ProductPackLevelMeasuredRecordPackLevelMeasurement with a new readingcom.warehouse.wms.product-master.product.ProductPackLevelMeasured

The Product aggregate also holds its PackagingHierarchy (ADR 0011): every pack level the SKU ever declared, retired ones kept. Its invariants: a GTIN is valid and identifies one (sku, level); an ACTIVE level's content quantity never changes; every level above EACH has an active lower child level and qtyOfChild of at least 2; a pack weighs at least its contents when both weights are known; at most one active level per PackLevel.

Each command raises at most one event, except DeclarePackLevel with a new GTIN at an active level, which raises two (Retired, then Declared, one version apart). The FanoutEncoder writes each unit event as two outbox rows (and each pack level event as one, on the integration topic only) in the same unit of work (one on warehouse.product-master.events, one on warehouse.product-master.analytics, same CloudEvents id; ADR 0006).

8. Throughput (estimate)​

Estimate, not measured. Writes are stewardship actions: registrations and classifications when SKUs are onboarded (bursts during a catalogue load), measurements when a unit passes a dimensioning device. One SKU is rarely written twice in the same second, so version conflicts should be rare. Reads by operators are occasional; services do not read it at request time.

9. Size (estimate)​

Estimate. One row per SKU, a fixed number of scalar columns plus a short tag array (at most five tags). The aggregate never grows: there is no measurement history and no child collection. Events per instance grow with its edit count; each is two outbox rows (integration and analytics topic).

Not aggregates: value objects and technical tables​

ModelKindWhereWhy it is not an aggregate
ClassificationValue objectclassification.goImmutable, replaced whole, no identity
UnitDimensions, Measurement, PhysicalProfileValue objectsphysical.goImmutable parts of Product, replaced whole
processed_eventsIdempotency guardmigration 0001, ProcessedEventRepoHeld the claims of the legacy importer; since stage E no inbound consumer of cmd/api claims into it (the repo is still constructed in cmd/api/adapters.go, with no caller)
outbox_eventsTransactional outboxmigration 0001, OutboxRepoEncoded messages awaiting the relay