Install
openclaw skills install bookforge-unit-of-work-implementerImplement Unit of Work (UoW) — the object that tracks new, dirty, clean, and removed entities during a business operation and commits all database changes together in the correct order. Use when asked: "how do I implement Unit of Work?", "how does Hibernate Session work under the hood?", "how do I avoid N+1 writes?", "how should I structure DbContext scoping?", "SQLAlchemy session management best practices", "EntityManager lifecycle", "ORM session management", "how to track entity changes in a Data Mapper layer?", "first-level cache", "identity map implementation", "object change tracking", "persistence coordination", "commit ordering with foreign keys", "how does EF Core SaveChanges work?", "dirty tracking", "how to batch database writes?", "UoW pattern implementation", "Hibernate Session vs EntityManager", "SQLAlchemy Session scope", "DbContext per-request", "entity state tracking". Applies when a Data Mapper pattern is in place (or being introduced) and the team needs disciplined change tracking, ordered commits, and first-level caching across a business operation. Integrates with Identity Map for cache and identity consistency. Integrates with Optimistic Offline Lock via version-conditioned UPDATE. Prerequisite: Data Mapper must be the chosen data-source pattern; UoW does not apply cleanly to Active Record (AR handles per-object persistence without a coordinator). If the data-source pattern has not been chosen, invoke `data-source-pattern-selector` first.
openclaw skills install bookforge-unit-of-work-implementerA Unit of Work (UoW) is the coordinator object that tracks every entity touched during a business operation — newly created, loaded and modified, or deleted — and then flushes all changes to the database together in the correct order inside a single system transaction.
Use this skill when:
Session, EF DbContext, SQLAlchemy Session) and want to understand the underlying contractPrerequisite: Data Mapper must be the chosen data-source pattern. If it has not been selected, invoke data-source-pattern-selector first, or ask the user to confirm their persistence approach before proceeding. UoW adds a coordination layer that Active Record codebases do not need.
data-source-pattern-selector.Order, LineItem, Product)?find() / insert() / update() / delete() methods will be called by the UoW on commit.Step 1 — Confirm Data Mapper prerequisite.
WHY: Unit of Work is designed to work with Data Mapper's separation of domain objects from SQL. Active Record embeds persistence in the entity itself; adding a UoW coordinator duplicates responsibility and creates confusion about who owns the save call. Confirming the prerequisite prevents a misapplication that will complicate the codebase.
data-source-pattern-selector if the team wants to reassess.data-source-pattern-selector or ask user directly.Step 2 — Choose the registration strategy.
WHY: The UoW must know which objects have changed. There are three strategies, each with a different trade-off between transparency and coupling. Choosing the wrong one for the stack and team leads to missed registrations (caller registration) or domain-layer coupling (object registration).
Evaluate each option:
| Strategy | How it works | Best for | Risk |
|---|---|---|---|
| Caller registration | Application code calls uow.registerDirty(entity) explicitly | Simple custom layers, greenfield | Easy to forget; silent data loss |
| Object registration | Entity setters call UoW.getCurrent().registerDirty(this) | Custom frameworks; Java/C# domain objects | Couples domain to UoW; requires access to current UoW |
| UoW-controlled (copy-on-load) | UoW registers clean objects on load; detects changes at commit via snapshot comparison | ORM-provided (Hibernate, EF, SQLAlchemy) | Higher memory overhead; infrastructure-heavy |
Decision:
Step 3 — Design the UoW API.
WHY: The interface is the contract between domain code and the persistence coordinator. Keeping it minimal and explicit prevents the UoW from becoming a god object.
Minimum API:
registerNew(entity) — entity will be INSERTed on commit
registerDirty(entity) — entity will be UPDATEd on commit
registerClean(entity) — entity is known, no action on commit; populates Identity Map
registerRemoved(entity) — entity will be DELETEd on commit
commit() — flush all changes in order, then DB COMMIT
rollback() — discard change sets; no DB writes
clear() — reset UoW state (call after commit or on request teardown)
Invariant assertions (enforce at registration time):
registerNew: entity must have a non-null ID; must not be in dirty or removed list.registerDirty: must not be in removed list; no-op if already in new list.registerRemoved: if in new list → just remove from new (no DB write needed); remove from dirty.Step 4 — Implement four-state tracking.
WHY: The four states map directly to the four SQL operations. Tracking state precisely prevents redundant SQL (e.g., updating an entity that was just inserted) and missed SQL (e.g., forgetting to delete an entity that was removed mid-operation).
Internal storage — three collections (clean objects are tracked only in Identity Map):
newObjects: List<DomainObject> → INSERT on commit
dirtyObjects: List<DomainObject> → UPDATE on commit
removedObjects: List<DomainObject> → DELETE on commit
identityMap: Map<(Class, Id), DomainObject> → first-level cache
State transition rules:
registerClean → add to identityMap.registerDirty → move to dirtyObjects.registerNew → add to newObjects AND identityMap.registerRemoved → move to removedObjects; remove from dirtyObjects.new object (not yet in DB) → remove from newObjects; no DB action needed.For detailed per-transition code examples see references/entity-state-transitions.md.
Step 5 — Implement the ordered commit procedure.
WHY: Database referential integrity requires that parent rows exist before child rows are inserted, and child rows are deleted before parent rows. Committing in arbitrary order produces FK violation errors. The UoW is the natural place to enforce this ordering because it holds the full change set.
Commit sequence:
newObjects in FK dependency order (parents before children). Use a topological sort of the FK graph for complex schemas; use explicit ordering for small schemas.dirtyObjects (order within this set is usually safe; touch each exactly once).removedObjects in reverse FK dependency order (children before parents).COMMIT on the system transaction.For the topological sort algorithm and ordering metadata approach see references/commit-ordering.md.
Step 6 — Wire Identity Map integration.
WHY: Without an Identity Map, loading the same entity twice produces two separate in-memory objects for the same database row. Updating both produces conflicting writes and undefined behavior. The Identity Map, co-located in the UoW, prevents this by ensuring every load returns the same instance.
Implementation:
(entityClass, primaryKey) tuple.find(class, id): check Identity Map first. If found → return cached instance. If not → load from DB, call registerClean, add to map, return.registerNew: add to map immediately (the new ID must be assigned before registration).registerRemoved: remove from map.Step 7 — Establish lifecycle management.
WHY: A UoW that spans multiple requests accumulates stale data, grows without bound, and causes race conditions when shared across threads. The lifecycle must be bounded.
Standard lifecycles:
Anti-pattern: never share a UoW across requests or threads. A shared UoW accumulates dirty objects from multiple users, produces incorrect commits, and leaks memory.
Step 8 — Integrate with collaborators.
WHY: UoW is rarely used in isolation. Two patterns depend on UoW for correct behavior; wiring them explicitly prevents integration bugs.
Optimistic Offline Lock integration:
version field (integer or timestamp).updateDirty, the UPDATE SQL becomes: UPDATE ... SET ..., version = version+1 WHERE id=? AND version=?rowsAffected == 0 → collision detected → raise ConcurrencyException, roll back transaction.optimistic-offline-lock-implementer for the full version-management workflow.Lazy Load integration:
Step 9 — Map to your stack's native UoW.
WHY: Most modern stacks include a built-in Unit of Work. Using it directly is far preferable to hand-rolling; the skill's value is understanding the contract so you configure and scope the built-in correctly.
| Stack | UoW Object | Registration strategy | Commit call |
|---|---|---|---|
| Hibernate (Java) | Session | UoW-controlled (snapshot) | session.flush() + tx.commit() |
| Spring Data JPA | EntityManager via @Transactional | UoW-controlled | transaction commit |
| EF Core (.NET) | DbContext | UoW-controlled (change tracker) | dbContext.SaveChanges() |
| SQLAlchemy (Python) | Session | UoW-controlled + explicit session.add() | session.commit() |
| TypeORM (TS/JS) | EntityManager / QueryRunner | UoW-controlled | queryRunner.commitTransaction() |
| Django ORM | No first-class UoW | Per-save explicit | transaction.atomic() wrapper |
For Django: use transaction.atomic() to batch saves, but note there is no central dirty tracker — bulk_update / bulk_create provides partial batching.
For stack-specific scoping patterns (request-scoped DbContext in ASP.NET, scoped Session in FastAPI, EntityManager lifecycle in Jakarta EE) see references/stack-native-uow-guide.md.
UoW Implementation Artifact (written to the codebase or returned inline):
## Unit of Work Implementation Record
### Registration Strategy
[Caller | Object | UoW-controlled] — [rationale]
### API
registerNew(entity) / registerDirty(entity) / registerClean(entity) / registerRemoved(entity)
commit() / rollback() / clear()
### State Tracking
- newObjects: [List<Entity>]
- dirtyObjects: [List<Entity>]
- removedObjects: [List<Entity>]
- identityMap: Map<(Class, Id), Entity>
### Commit Sequence
1. INSERT newObjects in order: [entity order based on FK graph]
2. UPDATE dirtyObjects
3. DELETE removedObjects in reverse order: [reverse FK order]
4. DB COMMIT
5. Clear UoW state
### Lifecycle
[Per-request | Per-command] — [where UoW is created and where it is discarded]
### Stack-Native Equivalent
[If using Hibernate/EF/SQLAlchemy: the built-in Session/DbContext IS the UoW.
Map register/commit calls to the framework's API.]
### Integration Notes
- Optimistic Offline Lock: [version column present / not applicable]
- Lazy Load: [proxy collections wired through session / not applicable]
### Anti-Patterns to Watch
- [ ] Cross-request UoW sharing
- [ ] Missing registerDirty calls (caller registration risk)
- [ ] FK ordering violations on commit
- [ ] UoW not cleared between requests → memory leak + stale data
1. UoW is the database change controller — not individual domain objects. Without a UoW, each domain object decides when to write to the database. This produces excessive round trips, inconsistent ordering, and no natural rollback point. The UoW centralizes that control: domain code mutates objects freely; the UoW decides when and in what order those mutations reach the database.
2. The four states (new, dirty, clean, removed) map exactly to the four SQL operations. Every entity in a business operation is in exactly one of these states. Understanding the state machine prevents double-writes, missed writes, and cascade ordering errors. The UoW enforces the state machine at registration time via assertions.
3. INSERT/DELETE order is determined by FK dependencies, not by the order changes were made.
If LineItem references Order, then Order must be inserted before LineItem, and LineItem must be deleted before Order. The UoW must encode or compute this graph. Ignoring it works until it doesn't — a single FK violation on commit surfaces the missing ordering logic.
4. Identity Map is not optional — it is required for correctness. Loading the same row twice into two objects is a correctness bug, not a performance issue. The Identity Map prevents this by making the UoW the single source of truth for in-memory entity identity. Performance caching is a beneficial side-effect, not the purpose.
5. UoW lifecycle must be bounded to one business operation. A UoW that outlives its business operation accumulates stale state and grows unboundedly. Cross-request sharing is especially dangerous in web apps because it causes different users' changes to be committed together. Enforce a clear begin/end boundary and discard the UoW after commit.
6. On modern stacks, use the built-in Session/DbContext — understand its contract, don't fight it.
Hibernate Session, EF Core DbContext, and SQLAlchemy Session implement the full UoW + Identity Map contract. The skill's purpose is to understand what they do (so you scope, flush, and clear them correctly) — not to replace them with a hand-rolled alternative.
Trigger: "We have a Java e-commerce service with Order, LineItem, and Product. We're using hand-rolled Data Mappers (no ORM). After a business operation touches 12 objects, we're making 12 separate UPDATE calls. How do we introduce a Unit of Work?"
Process:
Order and LineItem call UoW.getCurrent().registerDirty(this).registerNew / registerDirty / registerClean / registerRemoved / commit().(Class, Long id); populated on OrderMapper.find(id).UnitOfWork.newCurrent() on request start; UnitOfWork.getCurrent().commit() + setCurrent(null) on request end (in finally block).Output: Hand-rolled UnitOfWork class with three lists (new/dirty/removed), ThreadLocal storage for current UoW, DomainObject base class with markDirty() / markNew() / markRemoved(), and per-request lifecycle managed by a servlet filter. See references/entity-state-transitions.md for full Java sketch.
Trigger: "We use SQLAlchemy with a FastAPI app. We're seeing stale data and occasional DetachedInstanceError. How should we scope the Session?"
Process:
Session is the UoW.session.add(entity) registers new objects.Session is likely being shared across requests (application-scoped singleton) rather than per-request.Session per FastAPI request via Depends(get_db), where get_db yields a session and closes it after the request.session.commit() — SQLAlchemy resolves INSERT ordering via mapper relationships; session.flush() pushes SQL without committing for mid-operation ID resolution.DetachedInstanceError. Fix: load eagerly for data needed after session close, or keep session open for the request lifetime.Output: get_db generator dependency, per-request session scope, session.add for new entities, session.delete for removed, session.commit() at end of each request handler (or in a middleware). Anti-pattern warning: never use a module-level Session instance.
Trigger: "We have an ASP.NET Core app with EF Core. We're trying to understand when to call SaveChanges and how to avoid detached entity errors."
Process:
DbContext is the UoW; entities tracked by the change tracker.Scoped in ASP.NET Core DI → one instance per HTTP request. This is correct.await dbContext.SaveChanges() at the end of the service method (or in a controller action). Avoid calling it multiple times per request unless intentional.[Timestamp] or [ConcurrencyToken] property; EF Core adds WHERE version=? automatically and throws DbUpdateConcurrencyException on collision.DbContext from a scoped service into a singleton service → context outlives the request, accumulates stale data.Output: Confirm Scoped lifetime, single SaveChanges() call per business operation, [ConcurrencyToken] on entities needing optimistic locking, and warning against singleton-scoped DbContext.
references/entity-state-transitions.md — Full state machine with Java pseudocode for four entity states and registration assertionsreferences/commit-ordering.md — Topological sort algorithm for FK-ordered INSERT/DELETE + ordering for Order/LineItem/Product examplereferences/stack-native-uow-guide.md — Per-stack session scoping patterns (FastAPI, ASP.NET Core, Spring Boot, Jakarta EE, TypeORM)references/identity-map-implementation.md — Key design choices (explicit vs generic map, one map per class vs per session, inheritance handling)Related patterns triggered by this skill's output:
optimistic-offline-lock-implementerlazy-load-strategy-implementerdata-source-pattern-selectorThis skill is licensed under CC-BY-SA-4.0. Source: BookForge — Patterns of Enterprise Application Architecture by Martin Fowler et al.
Install related skills from ClawhHub:
clawhub install bookforge-data-source-pattern-selectorclawhub install bookforge-lazy-load-strategy-implementerclawhub install bookforge-optimistic-offline-lock-implementerOr install the full book set from GitHub: bookforge-skills