Urgent.News

What's breaking now, across thousands of outlets.

Tech

You Cloned the Aggregate. Why Is Your Read-Only Operation Still Changing State?

Imagine you have a domain operation that answers a very common question: “What would happen if we continued this trip from here?” You don't want to actually continue the trip. You only want to preview the result. Maybe you're calculating: the estimated arrival time, the remaining driving time, the stops that would be reached, or the driver's remaining compliance time. A natural solution is to…

In the article, the author presents a common problem when trying to preview the outcome of a domain operation without actually modifying the original state. The example used is a `Trip` aggregate, which contains a list of stops and the current progress of the trip. The scenario is as follows:

1. A `Trip` object has its own state, including stops and progress.

2. There is a domain service, `AdvanceRoute`, that simulates the progression of the trip by moving it forward and updating the trip's progress and the driver's compliance ledger.

The author mentions a first implementation where a clone of the `Trip` is created, and the `AdvanceRoute` method is called on the cloned object. The author suggests that this should prevent any changes to the original `Trip`, but this is not the case.

The problem arises because the `AdvanceRoute` method is not only modifying the `Trip` object but also the `DriverComplianceLedger` object, which is not a part of the `Trip` object graph. The author points out that the `Clone()` method of the `Trip` creates a deep copy of the `Trip`, copying all relevant state, but it does not clone the `DriverComplianceLedger` object.

The critical distinction the author makes is between the object graph (what belongs to the `Trip` aggregate) and the mutation boundary (what the operation can change). While a deep clone of the `Trip` correctly isolates the object graph, it does not isolate the mutation boundary. The domain logic changes the `DriverComplianceLedger`, which was not cloned, thus modifying the real production state.

In summary, the problem is that cloning an object does not necessarily prevent an operation from modifying state outside of its intended scope if that state is not part of the cloned object graph.

Written by urgent.news from Dev.to's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.

Read the original at dev.to →

More in Tech

South Africa: How Gauteng's New Panic App Exposed Your Data

[GroundUp] Reports about domestic violence and assault, including images and locations, could be accessed by a tech-savvy computer user

  • Gauteng's e-Panic app exposed users' personal data and crime reports.
  • App's background location feature and connections to Discord raised security concerns.

More from Wednesday 23 September →