The Unit of Work (UoW) pattern groups a set of database operations into a single atomic transaction and defers domain-event dispatch until the transaction succeeds. MASA Framework’sDocumentation Index
Fetch the complete documentation index at: https://mintlify.com/masastack/MASA.Framework/llms.txt
Use this file to discover all available pages before exploring further.
IUnitOfWork is the central coordinator between your EF Core DbContext, the IDomainEventBus, and the IEventBus pipeline — ensuring that domain events are only raised after data has been safely committed.
Installation
IUnitOfWork Interface
Properties
| Property | Type | Description |
|---|---|---|
ServiceProvider | IServiceProvider | The scoped service provider for this unit of work |
Transaction | DbTransaction | The active database transaction (throws if not begun) |
TransactionHasBegun | bool | Whether a transaction is currently open |
UseTransaction | bool? | Force on/off; null lets the provider decide per scenario |
DisableRollbackOnFailure | bool | When true, skips automatic rollback on exception |
EntityState | EntityState | UnChanged or Changed — whether EF has pending writes |
CommitState | CommitState | UnCommited (open transaction) or Commited |
Methods
| Method | Description |
|---|---|
SaveChangesAsync() | Persists tracked changes to the database via DbContext.SaveChangesAsync(). Also enqueues any pending domain events via IDomainEventBus. |
CommitAsync() | Commits the open transaction and calls IDomainEventBus.PublishQueueAsync() to dispatch all enqueued domain events. |
RollbackAsync() | Rolls back the transaction. Pending domain events are discarded with the transaction. |
EntityState and CommitState
ITransaction Interface
ITransaction is the marker interface that commands and events carry to propagate the IUnitOfWork through the handling pipeline.
IDomainEvent, DomainCommand, and the CQRS Command type all implement ITransaction, allowing the TransactionEventMiddleware to coordinate transactions automatically.
Registration
Via AddMasaDbContext (recommended)
Via EventBus builder
TransactionEventMiddleware
WhenUseUoW<TDbContext>() is configured, the EventBus automatically inserts TransactionEventMiddleware<TEvent> around every event handler. This middleware:
- Sets
UseTransaction = trueon theIUnitOfWorkif not already configured - Awaits the handler pipeline (
next()) - Calls
SaveChangesAsync()thenCommitAsync()on success - Calls
RollbackAsync()on any exception (unlessDisableRollbackOnFailure = true)
IUnitOfWork directly in command handlers. The middleware handles the full save-and-commit lifecycle.
Usage in Command Handlers
The typical pattern with CQRS — the UoW is managed entirely by the middleware:Manual UoW Control
For scenarios outside the EventBus pipeline — such as background jobs or scripts — injectIUnitOfWork directly:
Transaction Options
Control transaction behavior when registering the UoW:Setting
useTransaction: false is useful for read-heavy services or tests where transactional overhead is undesirable. Domain events are still dispatched via CommitAsync, but no database transaction is opened.