DCSIMG
Using Unit of Work Pattern with Entity Framework - Gil Fink's Blog

Gil Fink's Blog

Fink about IT

News

Microsoft MVP

My Facebook Profile My Twitter Profile My Linkedin Profile

Locations of visitors to this page

Creative Commons License

Disclaimer
The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.
© Copyright 2013 Gil Fink

Hebrew Articles

Index Pages

My OSS Projects

English Articles

Using Unit of Work Pattern with Entity Framework

Using Unit of Work Pattern with Entity Framework

In a previous postUsing Unit of Work Pattern with Entity Framework
I explained how
to create a
simple repository
on top of
Entity Framework.
In this post I’ll explain the Unit of Work pattern and how
we can use it with our data access layer.

What is Unit of Work Pattern?

In his famous and developer must read book
Patterns of Enterprise Application Architecture”, Martin Fowler
defines the Unit of Work as “Maintains a list of objects affected by a
business transaction and coordinates the writing out of changes and the
resolution of concurrency problems.”
What it means is that a unit of work is a context/session/unit object that
tracks the changes of business entities during one business transaction. It is
also responsible for the manage of concurrency problems when they occur.

How to Use The Pattern?

As in the repository pattern we will start with an interface which was
suggested by Martin Fowler:

public interface IUnitOfWork<T>
{
    void RegisterNew(T entity);
    void RegisterDirty(T entity);
    void RegisterClean(T entity);
    void RegisterDeleted(T entity);
    void Commit();
}

As you can see we register changes of entities inside the unit of work
and in the end of every transaction we commit it using the commit method.
In all the major ORMs out there you get all that functionality within the
session/context objects. It is the unit of work object that holds the details
of every change you do to an entity. Since this is the case you don’t
have to have all the four register methods when you use frameworks
like Entity Framework, NHibernate or other ORMs.
In my example I’m going to use the following unit of work interface:

public interface IUnitOfWork<T>
{
    void RegisterNew(T entity);
    void RegisterDeleted(T entity);
    void Commit();
}

Entity Framework Example

Lets return to the example of the repository I used in the previous post.
I can transform the DepartmentRepository that I’ve built into a
unit of work that handle departments like this one:

public class DepartmentRepository : IRepository<Department>,
    IUnitOfWork<Department>, IDisposable
{
    #region Members
 
    private SchoolEntities _context;        
 
    #endregion
 
    #region Ctor
 
    public DepartmentRepository() 
    {            
        _context = new SchoolEntities();
    }
 
    #endregion
 
    #region IRepository<Department> Members
 
    public Department GetById(int id)
    {
        return _context.Departments.
            Where(d => d.DepartmentID == id).
            FirstOrDefault();
    }
 
    public Department[] GetAll()
    {
        return _context.Departments.ToArray();
    }
 
    public IQueryable<Department> Query(Expression<Func<Department, bool>> filter)
    {
        return _context.Departments.Where(filter);
    }
 
    #endregion
 
    #region IUnitOfWork<Department> Members
 
    public void RegisterNew(Department entity)
    {
        _context.AddToDepartments(entity);
    }
 
    public void RegisterDeleted(Department entity)
    {
        _context.DeleteObject(entity);
    }
 
    public void Commit()
    {
        _context.SaveChanges();
    }
 
    #endregion
 
    #region IDisposable Members
 
    public void Dispose()
    {
        if (_context != null)
        {
            _context.Dispose();
        }
        GC.SuppressFinalize(this);
    }
 
    #endregion
}

This is a really simple example of how to implement the pattern.
There are a lot of other examples in the web like in the following link:
Entity Framework 4.0 Beta 1 – POCO, ObjectSet, Repository and UnitOfWork

Summary

Lets sum up, every ORM that respect itself (like Entity Framework) includes
implementation for the unit of work pattern. If we want to add our abstraction
layer which include a unit of work then we need to create our implementation.
We will start with an interface such as the one that Martin Fowler suggested.
Then it is our responsibility to track changes and to persist all the work that was
done in a time period as a transaction and also manage concurrency issues. 

DotNetKicks Image

Comments

DotNetKicks.com said:

You've been kicked (a good thing) - Trackback from DotNetKicks.com

# February 5, 2010 3:28 PM

Muhammad Mosa said:

Hi Gil, good stuff you have here.

I just wanted to share my opinion. Personally I don't think that repositories is a good place to keep track of change tracking, even through an embedded object context.

I guess Unit of Work shouldn't be implemented inside any repositories. On other hand unit of work controls all repositories. They all share same Object Context. but Only unit of work instance can submit changes.

Example

using(var uow = new UnitOfWork())

{

   var deptRepo = new DepartmentRepository();

   deptRepo.Add(new Department());

   Department dept = deptRepo.FindById(1);

   dept.Name = "some name";

   //We don't need to mark dirty for the above, as //the repository internally use an object context //that will track changes automatically

   var anotherRepo = new AnotherRepository();

   anotherRepo.Add(new Another());

  uow.SubmitChanges();

}

# February 5, 2010 6:55 PM

Yoann. B said:

Agreed with Muhammad Mosa too !

# February 6, 2010 4:14 AM

Gil Fink said:

Hi Muhammad Mosa and Yoann,

I agree with what you wrote.

The example is very simple and it's based only on one repository in the system. Since we have only one repository it will also be the unit of work. In enterprise applications we have more then one repository and therefore the implementation will change. The unit of work will wrap more then one repository since in a business transaction we share more then one entity. In this case the repository implementation will get the context from the outside probably from the unit of work container. Since I wanted to show a concept I wrote this easy example.

# February 6, 2010 9:27 AM

Using Unit of Work Pattern with Entity Framework - Gil Fink on .Net | Israel Today said:

Pingback from  Using Unit of Work Pattern with Entity Framework - Gil Fink on .Net | Israel Today

# February 7, 2010 10:38 PM

Gil Fink on .Net said:

Revisiting the Repository and Unit of Work Patterns with Entity Framework In the past I wrote two posts

# June 21, 2010 10:04 AM