Skip to content

Instantly share code, notes, and snippets.

@Quantumplation
Last active January 2, 2016 17:29
Show Gist options
  • Save Quantumplation/8337379 to your computer and use it in GitHub Desktop.
Save Quantumplation/8337379 to your computer and use it in GitHub Desktop.
/* Martin had this suggestion for an improvement */
public class UnitOfWork : IUnitOfWork
{
private UnitOfWork() {}
public static Do(Action<IUnitOfWork> action)
{
action(new UnitOfWork());
}
public T Get<T>() {}
}
public enum TransactionResult { Commit, Rollback };
public class MutableUnitOfWork : UnitOfWork, IMutableUnitOfWork
{
private MutableUnitOfWork(){}
public static Do(Func<IMutableUnitOfWork, TransactionResult> action)
{
try
{
if(action(new MutableUnitOfWork()) == TransactionResult.Commit)
Commit();
}
catch(Exception ex)
{
/* Log? */
}
Rollback();
}
public void Save<T>(T obj) {}
private void Commit() {}
private void Rollback() {}
}
public void SomeAction()
{
MutableUnitOfWork.Do(uow =>
{
DoStuff(uow);
var floor = uow.Get<Floor>(1);
floor.Name = "Test";
uow.Save<Floor>(floor);
if(SessionExpired)
return TransactionResult.Rollback;
return TransactionResult.Commit;
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment