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. 
    2. This is one of the most common design patterns in enterprise software development is the Unit of Work.
    3. It maintains a list of objects affected by a business transaction and coordinates the writing out of changes and the resolution of concurrency problems.
    4. 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.
    5. 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.
  3. 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 
  4. Implementation
    1. In most of the cases this pattern is implemented by ORM frameworks like Entity Framework
  5. Related Patterns
    1. Repository Pattern
  6. Related Technologies 
    1. Entity Framework : DBContext object of Entity Framework implement this pattern out of the box
  7. 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 ...
  8. References
    1. Overview Tutorials
    2. Further References
      1. MSDN:The Unit Of Work Pattern And Persistence Ignorance
      2. Implementing the Repository and Unit of Work Patterns in an ASP.NET MVC Application