a.Unit of Work Pattern

  1. Motivation

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

  2. Summary

    1. This pattern keeps track of everything that happens during a business transaction that affects the database. At the conclusion of the transaction, it determines how to update the database to conform to the changes.

      1. This is one of the most common design patterns in enterprise software development is the Unit of Work.

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

      3. The Unit of Work pattern isn't necessarily something that you will explicitly build yourself, but the pattern shows up in almost every persistence tool.

      4. The ITransaction interface in NHibernate, the DataContext class in LINQ to SQL, and the ObjectContext class in the Entity Framework are all examples of a Unit of Work. For that matter, the venerable DataSet can be used as a Unit of Work.

  1. When to Use

      1. Usually pattern is implemented by data access framework such as entity frameworks.

      2. Alternatively it can be explicitly be used when several transaction are modifying business object and you need to track them until changes are pushed to database

  1. Implementation

      1. In most of the cases this pattern is implemented by ORM frameworks like Entity Framework

  1. Related Patterns

      1. Repository Pattern

  1. Related Technologies

      1. Entity Framework : DBContext object of Entity Framework implement this pattern out of the box

  1. Specific Considerations

    1. Unit of Work Vs Repository Pattern

        1. Usually Unity of Work is implemented by data access framework like entity framework and repositories usually consume them.

        2. In other words repositories are used to create business objects and Unit of Work is used to track the changes made by business transactions to the business objects. Read More ...

  1. References