Created
April 4, 2012 12:39
-
-
Save pawelpabich/2300832 to your computer and use it in GitHub Desktop.
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
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