Has anyone used xstate for domain modeling? #3398
Replies: 1 comment 1 reply
-
DDD is a very interesting topic where I think state machines fit naturally into, and admittedly one that I need to research more! I agree that state machines are a great way to represent the state of individual entities, and statecharts in particular can model how these entities interact with each other, by sending messages. To answer this question:
What you can do is add a wildcard transition that will catch unknown events in the root-level state: This is the recommended pattern, and gives you full control over what happens with unrecognized events. |
Beta Was this translation helpful? Give feedback.
-
In Domain Driven Development (DDD), there are often domain entities that have complex lifecycles which can be difficult to manage or express to non-technical members of the team. These entities often bear a lot of resemblance to state machines. I'm curious if anyone has used xstate and its state charts to articulate the essential complexity of their domain entities as well as to protect against invariants within their systems.
You could imagine a state machine being handed a domain entity, and then synchronizing its state to that of the entity. And then, in order to operate on the entity, the state machine acts as a gatekeeper of sorts, and mutations of the domain entity are only allowed by sending events to the machine. You may want to treat interactions with the machine as an infrastructure concern. The other thing I'm not sure how you'd solve is catching illegal operations sent to the machine. If you send an event to a machine that isn't in the state it needs to be in order to consume the event, nothing happens. But in this example, you may want to detect this and handle it as some kind of error.
Here is a contrived example of how you could express the complex lifecycle of a "work order" from its creation to when payment is received and it is considered "complete".
Beta Was this translation helpful? Give feedback.
All reactions