Skip to content

Instantly share code, notes, and snippets.

@shawnmclean
Created July 4, 2013 16:00
Show Gist options
  • Save shawnmclean/5928810 to your computer and use it in GitHub Desktop.
Save shawnmclean/5928810 to your computer and use it in GitHub Desktop.
Interface for Unit Of Work
/// <summary>
/// Interface for the unit of work pattern, inherits from IDisposable to dispose of the DbContext
/// </summary>
public interface IUnitOfWork: IDisposable
{
/// <summary>
/// Saves the changes of the DbContext wrapped by the UoW
/// </summary>
/// <returns></returns>
int SaveChanges();
}
/// <summary>
/// Concrete class for the unit of work pattern, inherits from IDisposable to dispose of the DbContext
/// </summary>
public class UnitOfWork : IUnitOfWork
{
private readonly DbContext context;
private bool disposed;
#region IDisposable Members
/// <summary>
///
/// </summary>
/// <param name="context">the db context to wrap</param>
public UnitOfWork(DbContext context)
{
this.context = context;
}
/// <summary>
///
/// </summary>
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
#endregion
/// <summary>
/// Saves the changes of the DbContext wrapped by the UoW
/// </summary>
/// <returns></returns>
public int SaveChanges()
{
return context.SaveChanges();
}
/// <summary>
/// Disposes of the context
/// </summary>
/// <param name="disposing"></param>
protected virtual void Dispose(bool disposing)
{
if (!disposed)
{
if (disposing)
{
context.Dispose();
}
}
disposed = true;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment