Skip to content

Instantly share code, notes, and snippets.

@rofr
Created March 28, 2014 22:38
Show Gist options
  • Select an option

  • Save rofr/9844475 to your computer and use it in GitHub Desktop.

Select an option

Save rofr/9844475 to your computer and use it in GitHub Desktop.
What a lame name, help me out here!
/// <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);
}
}
@srdjan
Copy link
Copy Markdown

srdjan commented Mar 29, 2014

maybe a matter of taste - but here it goes... :)

public class Immutability : OptimisticKernel {

public Immutability(EngineConfiguration config, Model model) :
    base(config, model)  { }

public override object Execute(Command command)  { }


protected override void Ensure(ref object result, IOperationWithResult operation)
{  }

void Unpack(object tuple, out Model model, out object result)
{  }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment