Last active
January 2, 2016 17:29
-
-
Save Quantumplation/8337379 to your computer and use it in GitHub Desktop.
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
/* 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