Skip to content

Instantly share code, notes, and snippets.

@pawelpabich
Created April 4, 2012 12:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pawelpabich/2300832 to your computer and use it in GitHub Desktop.
Save pawelpabich/2300832 to your computer and use it in GitHub Desktop.
Unit of work
public class UnitOfWork : IUnitOfWork, IDisposable
{
private readonly MyContext context;
private bool isRolledback;
private readonly TransactionScope transactionScope;
public UnitOfWork(MyContext context)
{
this.context = context;
isRolledback = false;
transactionScope = new TransactionScope(TransactionScopeOption.Required);
}
public void Dispose()
{
try
{
if (!isRolledback)
{
context.SaveChanges();
transactionScope.Complete();
}
}
finally
{
transactionScope.Dispose();
}
}
public void Rollback()
{
if (!isRolledback)
{
isRolledback = true;
transactionScope.Dispose();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment