Click here to Skip to main content
15,890,512 members
Articles / Programming Languages / C#

Unit of Work Sample Implementation for ADO.NET

Rate me:
Please Sign up or sign in to vote.
4.09/5 (6 votes)
10 Jan 2014LGPL31 min read 37K   14   10
How to create a Unit Of Work implementation for ADO.NET

This article demonstrates how you can create a Unit Of Work implementation for ADO.NET.

I’ve got a couple of questions in my “Repository pattern” article about a unit of work implementation. Here is a sample.

This is the actual UoW factory. Notice that it has no abstraction more than returning IUnitOfWork from the method. No need to create an abstract factory as the actual creation as it is trivial.

C#
public class UnitOfWorkFactory
{
	public static IUnitOfWork Create()
	{
		var connection = new SqlConnection
         (ConfigurationManager.ConnectionStrings("MyDb").ConnectionString);
		connection.Open();
		
		return new AdoNetUnitOfWork(connection, true);
	}
}

The unit of work itself uses the interfaces defined in System.Data to be completely driver independent. That makes it quite easy to switch DB as long as you don’t use anything but the SQL92 standard in your SQL statements.

C#
public class AdoNetUnitOfWork : IUnitOfWork
{
	public AdoNetUnitOfWork(IDbConnection connection, bool ownsConnection)
	{
		_connection = connection;
		_ownsConnection=ownsConnection;
		_transaction = connection.BeginTransaction();
	}
	
	public IDbCommand CreateCommand()
	{
		var command = _connection.CreateCommand();
		command.Transaction = _transaction;
		return command;
	}
	
	public void SaveChanges()
	{
		if (_transaction == null)
			throw new InvalidOperationException
             ("Transaction have already been committed. Check your transaction handling.");
			
		_transaction.Commit();
		_transaction = null;
	}
	
	public void Dispose()
	{
		if (_transaction != null)
		{
			_transaction.Rollback();
			_transaction = null;
		}
		
		if (_connection != null && _ownsConnection)
		{
			_connection.Close();
			_connection = null;
		}
	}
}

Usage:

C#
using (var uow = UnitOfWorkFactory.Create())
{
	var repos = new UserRepository(uow);
	
	uow.SaveChanges();
}

The above code requires that the repositories break the Liskov's Substitution Principle in theory. But in reality, that will never be a problem unless you switch data layer entirely. But in that case, the simplest thing is to adjust the UnitOfWorkFactory so that it returns the new implementation.

Hence, do something like this in the repository:

C#
public class UserRepository
{
	private AdoNetUnitOfWork _unitOfWork;
	
	public UserRepository(IUnitOfWork uow)
	{
		if (uow == null)
			throw new ArgumentNullException("uow");
			
		_unitOfWork = uow as AdoNetUnitOfWork;
		if (_unitOfWork == null)
			throw new NotSupportedException("Ohh my, change that UnitOfWorkFactory, will you?");
	}
	
	public User Get(Guid id)
	{
		using (var cmd = _unitOfWork.CreateCommand())
		{
			cmd.CommandText = "SELECT * FROM Users WHERE Id = @id");
			cmd.AddParameter("id", id);
			
			// uses an extension method which I will demonstrate in a 
			// blog post in a couple of days
			return cmd.FirstOrDefault<User>();
		}
	}
}

My motivation to now abstract away the implementation in the repositories is that unit tests can never guarantee that the repositories work. Mocking/stubbing/faking will never work as the read database. Hence, you need to use integration tests (= use a DB) to be sure that your repositories work as expected.

That’s it!

License

This article, along with any associated source code and files, is licensed under The GNU Lesser General Public License (LGPLv3)


Written By
Founder 1TCompany AB
Sweden Sweden

Comments and Discussions

 
QuestionWhat is IUnitOfWork? Pin
dtekchandani1-Sep-16 4:14
dtekchandani1-Sep-16 4:14 
AnswerRe: What is IUnitOfWork? Pin
Cool Smith1-May-19 4:05
Cool Smith1-May-19 4:05 
QuestionHow to use UserRepository class Pin
Tridip Bhattacharjee2-Jan-16 5:35
professionalTridip Bhattacharjee2-Jan-16 5:35 
QuestionNot sure of the point Pin
Jeremy Stafford 122-Oct-15 16:50
Jeremy Stafford 122-Oct-15 16:50 
AnswerRe: Not sure of the point Pin
jgauffin22-Oct-15 20:40
jgauffin22-Oct-15 20:40 
GeneralRe: Not sure of the point Pin
Jeremy Stafford 17-Nov-15 12:08
Jeremy Stafford 17-Nov-15 12:08 
GeneralMy vote of 3 Pin
Sk8tz15-Jan-14 22:19
professionalSk8tz15-Jan-14 22:19 
GeneralRe: My vote of 3 Pin
jgauffin15-Jan-14 23:11
jgauffin15-Jan-14 23:11 
SuggestionRe: My vote of 3 Pin
Sk8tz20-Jan-14 5:36
professionalSk8tz20-Jan-14 5:36 
Hi
Not to be nit picky, you do have a good start, however for a 5, I would like to see possible variations, and extendability around the UOW you created, that is possible specifically with ADO.NET. Whats the diff UOW out there, what are the pros and cons.

Below is an example of a UOW I did for EF, for you to look at

XML
namespace Rms.Data
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;

    public abstract class GenericUnitOfWork<T> : IUnitOfWork<T>
         where T : BaseContext<T>
    {
        private T context;

        /// <summary>
        /// Initializes a new instance of the <see cref="GenericUnitOfWork{T}"/> class.
        /// </summary>
        public GenericUnitOfWork()
        {
            context = Activator.CreateInstance<T>();
            ConfigDefaultBehaviour();
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="GenericUnitOfWork{T}"/> class.
        /// </summary>
        /// <param name="context">The context.</param>
        public GenericUnitOfWork(T context)
        {
            this.context = context;
            ConfigDefaultBehaviour();
        }

        private void ConfigDefaultBehaviour()
        {
            context.Configuration.LazyLoadingEnabled = true;
        }

        /// <summary>
        /// Gets the context.
        /// </summary>
        /// <value>
        /// The context.
        /// </value>
        public T Context
        {
            get
            {
                return context;
            }
        }

        /// <summary>
        /// Commits this instance.
        /// </summary>
        public int Commit(string userId = "", int companyId = 0)
        {
            int recordsAffected = 0;

            // The function was successful, so we can save all
            // changes and complete the transaction.
            context.LoggedOnUserId = userId;
            context.CompanyId = companyId;
            recordsAffected = context.SaveChanges();

            return recordsAffected;
        }

        /// <summary>
        /// Gets or sets a value indicating whether lazy loading is enabled.
        /// </summary>
        /// <value>
        /// <c>true</c> if lazy loading is enabled; otherwise, <c>false</c>.
        /// </value>
        public bool LazyLoadingEnabled
        {
            get
            {
                return context.Configuration.LazyLoadingEnabled;
            }
            set
            {
                context.Configuration.LazyLoadingEnabled = value;
            }
        }

        private bool disposed;

        /// <summary>
        /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
        /// </summary>
        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }

        /// <summary>
        /// Releases unmanaged and - optionally - managed resources.
        /// </summary>
        /// <param name="disposing"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param>
        protected virtual void Dispose(bool disposing)
        {
            if (!disposed)
            {
                if (disposing && context != null)
                {
                    context.Dispose();
                    context = null;
                }
                disposed = true;
            }
        }

        /// <summary>
        /// Finalizes an instance of the <see cref="GenericUnitOfWork{T}"/> class.
        /// </summary>
        ~GenericUnitOfWork()
        {
            Dispose(false);
        }

    }
}


XML
sing Rms.Interface;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Rms.Data.AdminContext
{
    ///// <summary>
    ///// Summary Of Work for Administration Application Function
    ///// </summary>
    public class UnitOfWorkAdmin : GenericUnitOfWork<AdminContext>
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="UnitOfWorkAdmin"/> class.
        /// </summary>
        public UnitOfWorkAdmin()
            : base()
        {

        }

        /// <summary>
        /// Initializes a new instance of the <see cref="UnitOfWorkAdmin"/> class.
        /// </summary>
        /// <param name="context">The administration context.</param>
        public UnitOfWorkAdmin(AdminContext context)
            : base(context)
        {

        }

        // --------- Repositories ---------
        private IUserRepository user;

        /// <summary>
        /// Gets the user.
        /// </summary>
        /// <value>
        /// The user.
        /// </value>
        public IUserRepository User
        {
            get { return user ?? (user = new UserRepository(this.Context)); }
        }

        private IEmailRepository email;

        public IEmailRepository Email
        {
            get { return email ?? ( email = new EmailRepository( this.Context ) ); }
        }


        private ITaxClassRepository taxclass;

        public ITaxClassRepository TaxClass
        {
            get { return taxclass ?? ( taxclass = new TaxClassRepository( this.Context ) ); }
        }


    }

}

Sk8tZ

QuestionNice one! Pin
Dan Mordechay11-Jan-14 22:36
Dan Mordechay11-Jan-14 22:36 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.