Transaction Script
Escrito por Darjus Kazakevic · revisado el
One procedure per request, from top to bottom.
A transaction script handles one request as one procedure: check the input, read data, apply the rule, write, return. There is no domain model. For a lot of software this is the right answer. An admin form over one table does not need entities, repositories and a service layer.
Its weakness is shared rules. Each script holds its own copy, so a refund window can end up in three places with three values. That duplication is the signal to grow a domain model. Wait for it instead of guessing in advance.
En esencia — Write each business action as one straight procedure from input to database: the right amount of structure for CRUD work, until rules start to repeat.
Cuándo recurrir a él
Recurre a él cuando
- CRUD-style work in a supporting area with few, stable rules
- Each request is one action, with little logic shared between actions
- A small team or a prototype that needs to ship quickly
Reconsidéralo cuando
- The same rule already lives in a second script, and the copies drift
- The core domain, where rules interact and change often
- Entities with many states and transitions that need guarding
The parts
- Script
- One function per business action, such as cancelSubscription or approveInvoice. It reads from top to bottom and contains everything that action needs.
- Transaction boundary
- The script opens one database transaction, does all its writes inside it, and commits. Either every change happens or none does.
- Shared helpers
- Scripts may share data-access helpers, such as a query function. Sharing how to read a row is fine; copying a business decision between scripts is the warning sign.
- The duplication signal
- When the same rule, such as “refunds within 14 days”, appears in a second script, the copies start to disagree. That is when to move the rule into a domain object both can call.
Reglas de diseño
- Grow a model when a rule repeats, not before
- Duplication is the trigger, not line count. When a rule like the refund window appears in a second script, move it into a domain model. Building one earlier is cost with no return.
- Keep each script flat and transactional
- Check, do the work, write inside one transaction, return. Avoid helper layers: the value is that you can read the whole action in one go.
- Make retried scripts idempotent
- If a script can run twice (a double click, a retried job), check the current state first. Cancelling an already cancelled subscription should do nothing, not refund twice.
- Name scripts after the business action
- Use approveInvoice, not updateInvoice. The name says what the business does, so readers can find the rule and duplicates become visible.
Errores habituales
- Copied rules that drift. The API allows a 14-day refund and the admin screen allows 30.
- Scripts that call other scripts. Transactions nest, and nobody knows who owns each write.
- Building a full domain model too early: five classes and three interfaces for an edit form.