Unit of work pattern

From CSSEMediaWiki
Revision as of 07:41, 19 October 2010 by James Ashford (Talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Maintains a list of objects affected by a business transaction and coordinates the writing out of changes and the resolution of concurrency problems.

Often in a business you need to make a lot of operations in a single transaction, if the entire process successful then we can commit everything to the database, otherwise (if something fails) we need to rollback all our changes. Rather than having a persistant transaction running, we can use the Unit of work pattern to handle it.

How it works

A single class called a Unit of work is created, and a new instance is created for each transaction. Each operation is added to the Unit of work object, and if the entire operation is successful it will commit the entire transaction to the database.

You can also register read events which can be used by the Unit of work class to detect dirty reads.

When to use it

When you need to keep track of various objects that have been manipulated. It could be used with any database system (not only relational database)

Example

Personal tools