Created
July 4, 2013 16:00
-
-
Save shawnmclean/5928810 to your computer and use it in GitHub Desktop.
Interface for Unit Of Work
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// <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(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// <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