What's UOW?
[TOC]
What’s UOW?
“A Unit of Work keeps track of everything you do during a business transaction that can affect the database. When you’re done, it figures out everything that needs to be done to alter the database as a result of your work.” – Martin Fowler
SO Unit of Work Key Info:
- UOW keeps track of changes
- UOW maintenance change list
- UOW commits all changes at once
EF Core DbContext naturally UOW is realized
Each DbContext instance has a ChangeTracker that tracks changes to the entity
When DbContext.SaveChanges is called, all changes are committed to the database in one go via the transaction
Implementing Repository pattern in domain-driven Design,By UOW UnitOfWorkInterceptor took over after ApplicationServervice methods performed DbContext. SaveChanges operation
The transaction is just a way to realize the unit of work. EFCore can implement UOW without using transactions.
What’s Ambient Transactions?
Common Transaction:
1 | using (var context = new ApplicationDbContext()) |
Ambient Transaction:
An ambient transaction is one that works at the thread level. Thus, all operations that occur in that context will be part of the transaction.
1 | static void Main(string[] args) |
Reference:
P of EAA: Unit of Work (martinfowler.com)
DbContext Lifetime, Configuration, and Initialization - EF Core | Microsoft Learn
Transactions - EF Core | Microsoft Learn
Implementing an Implicit Transaction using Transaction Scope - .NET Framework | Microsoft Learn