Last active
October 25, 2017 06:53
-
-
Save andreabalducci/888bb2f414a812f6ad488d7ae30aa83b to your computer and use it in GitHub Desktop.
Process Managers @ NStore | Work in progress
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
using System; | |
using System.Collections; | |
// ReSharper disable UnusedMember.Local | |
#pragma warning disable S1172 // Unused method parameters should be removed | |
#pragma warning disable S1144 // Unused private types or members should be removed | |
namespace NStore.Domain.Tests.ProcessManagerTests | |
{ | |
public class CheckoutState | |
{ | |
public bool PaymentReceived { get; private set; } | |
public bool Shipped { get; private set; } | |
private IEnumerable StartedBy(OrderPlaced e) | |
{ | |
yield return new RequestPayment(e.OrderId) | |
.AndSignalTimeout() | |
.ToSelf() | |
.After(TimeSpan.FromHours(1)); | |
yield return new SendPurchaseConfirmation(e.OrderId); | |
} | |
private ShipOrder ContinuedBy(PaymentReceived e) | |
{ | |
this.PaymentReceived = true; | |
return new ShipOrder(e.OrderId); | |
} | |
private object On(MessageAndTimeout<RequestPayment> e) | |
{ | |
if (!this.PaymentReceived) | |
{ | |
if (e.Counter < 5) | |
{ | |
return e.RetryTimeoutAfter(TimeSpan.FromHours(e.Counter)); | |
} | |
return new CancelOrder(e.Message.OrderId, "Payment timed out"); | |
} | |
return null; | |
} | |
private object On(PaymentRequested e) | |
{ | |
this.PaymentReceived = true; | |
return new CheckPaymentReceived(e.OrderId) | |
.HappensAfter(e.TimeStamp.AddDays(1)); | |
} | |
private object On(CheckPaymentReceived e) | |
{ | |
if (!this.PaymentReceived) | |
{ | |
return new CancelOrder(e.OrderId, "Payment timeout"); | |
} | |
return null; | |
} | |
private void CompletedBy(OrderShipped e) | |
{ | |
this.Shipped = true; | |
} | |
} | |
} | |
#pragma warning restore S1144 // Unused private types or members should be removed | |
#pragma warning restore S1172 // Unused method parameters should be removed | |
// ReSharper restore UnusedMember.Local |
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
namespace NStore.Domain | |
{ | |
public sealed class ProcessManagerPayloadProcessor : IPayloadProcessor | |
{ | |
public static readonly ProcessManagerPayloadProcessor Instance = new ProcessManagerPayloadProcessor(); | |
private readonly string[] _methods = { "On", "StartedBy", "ContinuedBy", "CompletedBy" }; | |
public object Process(object state, object payload) | |
{ | |
return state.CallNonPublicIfExists(_methods, payload); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment