sajad torkamani

What is it?

The Unit of Work (UoW) pattern is a design pattern where instead of changing the database with each change to each entity, you create a Unit of Work object to keep track of all the changes.

When youโ€™re ready to commit the changes, the Unit of Work decides what to do and how to do it in the most efficient way. It opens a transaction, does any concurrency checking, and writes the changes to the underlying database.

Why bother with it?

The UoW patterns gives you two main benefits:

  • Faster writes: you commit all the changes in a single database operation rather than multiple.
  • Transactional integrity: combining multiple changes in a single transaction makes it easier to rollback all the changes if one fails instead of having partial updates where some updates succeeded by others failed. Partial updates can lead to invalid states in your database which can then cause all sorts of problems for your applications.

Other notes

  • The Unit of Work pattern is heavily used in ORM frameworks like Doctrine, Entity Framework, and Hibernate).

Sources

Leave a comment

Your email address will not be published. Required fields are marked *