← All writing
/writing

Your State Machine Should Be Data, Not a Workflow Engine

Most systems that model a process reach for one of two extremes: a big switch on an enum, or a full workflow engine. Both hurt, in opposite directions.

The problem

A hard-coded state machine means every new status is a release. A workflow engine means a second runtime, a second mental model, and a designer nobody on the team wants to own.

The split that worked

Put what in the database and how in code:

  • Which statuses exist, and which moves are permitted between them — rows.
  • What must be true before a move is allowed — named predicates in C#.

A transition row references its preconditions by key:

public interface ITransitionGuard
{
    string Key { get; }                        // matches a key on the transition row
    GuardResult Evaluate(GuardContext context);
}

Adding a rule becomes a row plus, at most, one new class. Adding a status becomes a row and nothing else.

Why it matters

The engine never reads your status constants. Those exist for guards, seeders and tests. That's what keeps the table authoritative — the moment code branches on if (status == Approved), the database has stopped being the source of truth and you have two of them.

One caution: this only works if the table is validated. A transition naming a precondition that no longer exists must be caught loudly, not skipped silently. More on that later in the series.

← All writing