Skip to content

Instantly share code, notes, and snippets.

@tomstaijen
Last active December 14, 2015 09:09
Show Gist options
  • Save tomstaijen/5063056 to your computer and use it in GitHub Desktop.
Save tomstaijen/5063056 to your computer and use it in GitHub Desktop.
public class MySaga :StatefulSaga<Henkie>
{
public MySaga(SerializationInfo info, StreamingContext context) : base(info, context)
{
}
public MySaga(Guid correlationId) : base(correlationId)
{
}
public void Handle(ParentCreated e)
{
State = new Henkie()
{
Rows = e.Children.ToList()
};
// sending ProcessChild will result in ChildDone events
State.Rows.ForEach(r => Bus.Publish(new ProcessChild(CorrelationId, r)));
}
public void Handle(ChildDone e)
{
if (LockFailed)
{
Bus.Publish(e);
}
else
{
//Thread.Sleep(5000);
State.Rows.Remove(e.ChildId);
if (!State.Rows.Any())
{
Bus.Publish(new AllDone(this.CorrelationId));
}
}
}
public void Handle(AllDone e)
{
// more work on the parent level here.
}
static MySaga()
{
Define(() =>
{
Initially(
When(Created)
.Then((s, m) => s.Handle(m))
.TransitionTo(New)
);
During(New,
When(ChildDone)
.Then((s, m) => s.Handle(m))
,
When(AllDone)
.Then((s,m) => s.Handle(m))
.Complete()
);
RemoveWhen(x => x.CurrentState == Completed);
});
}
#region states/events
public static Event<ParentCreated> Created { get; set; }
public static Event<ChildDone> ChildDone { get; set; }
public static Event<AllDone> AllDone { get; set; }
public static State Initial { get; set; }
public static State New { get; set; }
public static State Completed { get; set; }
#endregion
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment