Created
March 28, 2014 22:38
-
-
Save rofr/9844475 to your computer and use it in GitHub Desktop.
What a lame name, help me out here!
This file contains hidden or 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> | |
| /// A Kernel that handles immutable models and commands using lock free mvcc | |
| /// </summary> | |
| public class ImmutabilityKernel : OptimisticKernel | |
| { | |
| public ImmutabilityKernel(EngineConfiguration config, Model model) : | |
| base(config, model) | |
| { | |
| } | |
| public override object ExecuteCommand(Command command) | |
| { | |
| Model modelOut = null; | |
| object result = null; | |
| try | |
| { | |
| if (command is IImmutabilityCommand) | |
| { | |
| var typedCommand = command as IImmutabilityCommand; | |
| modelOut = typedCommand.Execute(_model); | |
| } | |
| else if (command is IImmutabilityCommandWithResult) | |
| { | |
| var typedCommand = command as IImmutabilityCommandWithResult; | |
| var tuple = typedCommand.Execute(_model); | |
| UnpackTuple(tuple, out modelOut, out result); | |
| } | |
| else throw new InvalidOperationException("Command type not supported by this kernel"); | |
| if (modelOut == null) throw new InvalidOperationException("Command returned null model"); | |
| _model = modelOut; | |
| return result; | |
| } | |
| catch (Exception ex) | |
| { | |
| throw new CommandAbortedException("Command failed, see inner exception for details", ex); | |
| } | |
| } | |
| protected override void EnsureNoMutableReferences(ref object result, IOperationWithResult operation) | |
| { | |
| //noop, result is immutable and so is model, no cloning necessary | |
| } | |
| private void UnpackTuple(object tuple, out Model model, out object result) | |
| { | |
| var type = tuple.GetType(); | |
| model = (Model)type.GetProperty("Item1").GetGetMethod().Invoke(tuple, null); | |
| result = type.GetProperty("Item2").GetGetMethod().Invoke(tuple, null); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
maybe a matter of taste - but here it goes... :)
public class Immutability : OptimisticKernel {