The **Unit of Work** pattern is responsible for keeping track of everything you've done during a business transaction that can affect the database.
When the transaction ends, the Unit of Work calculates everything that needs to be done to reflect the changes in the database (insertions, updates, and deletions).
# Why use Unit of Work?
Without this pattern, you might end up making multiple small calls to the database, which is inefficient. Unit of Work groups all operations into a single block, optimizing performance and ensuring the transaction is atomic.
# Pros
-
verified
Atomic Transactions
Ensures that all changes are saved or none.
-
verified
Reduction of DB Hits
Minimizes the number of connections to the database.
# Conceptual Example
1. uow.registerNew(customer)
2. uow.registerDirty(order302)
3. uow.commit() // Executes everything