Skip to content

Instantly share code, notes, and snippets.

@jkonecki
Last active November 10, 2018 03:11
Show Gist options
  • Save jkonecki/26b5bec619757e199e2d to your computer and use it in GitHub Desktop.
Save jkonecki/26b5bec619757e199e2d to your computer and use it in GitHub Desktop.
Orleans EventSourcing design
using System.Threading.Tasks;
using Orleans;
namespace MyApp
{
public class MyState
{
public string Foo { get; set; }
public int Bar { get; set; }
}
public class MyGrain : Grain<MyState>
{
public Task Initialize(string foo, int bar)
{
this.State.Foo = foo;
this.State.Bar = bar;
return this.WriteState();
}
public Task DoubleBar()
{
this.State.Bar *= 2;
return this.WriteState();
}
}
}
using System.Threading.Tasks;
namespace Orleans
{
public class Grain
{
}
public class StatefulGrain<T> : Grain
{
public T State { get; private set; }
}
public class Grain<T> : StatefulGrain<T>
{
public Task ReadState<T>()
{
return TaskDone.Done;
}
public Task WriteState<T>()
{
return TaskDone.Done;
}
public Task ClearState<T>()
{
return TaskDone.Done;
}
}
public class JournaledGrain<T> : StatefulGrain<T>
{
public Task RaiseEvent(object @event)
{
return TaskDone.Done;
}
}
}
using System.Threading.Tasks;
using Orleans;
namespace MyApp
{
public class Initialized
{
public Initialized(string foo, int bar)
{
this.Foo = foo;
this.Bar = bar;
}
public string Foo { get; }
public int Bar { get; }
}
public class BarDoubled
{
public BarDoubled(int oldBar, int newBar)
{
this.OldBar = oldBar;
this.NewBar = newBar;
}
public int OldBar { get; }
public int NewBar { get; }
}
public class MyJournaledState
{
public string Foo { get; private set; }
public int Bar { get; private set; }
private void Apply(Initialized @event)
{
state.Foo = @event.Foo;
state.Bar = @event.Bar;
}
private void Apply(BarDoubled @event)
{
this.Bar = @event.NewBar;
}
}
public class MyJournaledGrain : JournaledGrain<MyJournaledState>
{
public Task Initialize(string foo, int bar)
{
return this.RaiseEvent(new Initialized(foo, bar));
}
public Task DoubleBar()
{
return this.RaiseEvent(new BarDoubled(this.State.Bar, this.State.Bar * 2));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment