Created
July 20, 2012 08:50
-
-
Save udidahan/3149693 to your computer and use it in GitHub Desktop.
Saga code from ADSD course in Israel
This file contains 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 Billing | |
{ | |
public class RefundPolicyData : IContainSagaData | |
{ | |
public Guid Id { get; set; } | |
public string Originator { get; set; } | |
public string OriginalMessageId { get; set; } | |
public Guid OrderId { get; set; } | |
public DateTime TimeOfOrder { get; set; } | |
} | |
public class RefundPolicy : Saga<RefundPolicyData>, | |
IAmStartedByMessages<OrderAccepted>, | |
IHandleMessages<ProductsReturned> | |
{ | |
public TimeSpan FirstWindow { get; set; } | |
public TimeSpan SecondWindow { get; set; } | |
public void Handle(OrderAccepted message) | |
{ | |
Data.TimeOfOrder = message.TimeOfOrder; | |
} | |
public void Handle(ProductsReturned message) | |
{ | |
Bus.Send<IssueRefund>(m => { m.Percent = 100; m.OrderId = message.OrderId; }); | |
MarkAsComplete(); | |
} | |
public override void ConfigureHowToFindSaga() | |
{ | |
ConfigureMapping<ProductsReturned>(s => s.OrderId, m => m.OrderId); | |
} | |
} | |
public class IssueRefund : ICommand | |
{ | |
public Guid OrderId { get; set; } | |
public float Percent { get; set; } | |
} | |
public class ProductsShipped : IEvent | |
{ | |
public Guid OrderId { get; set; } | |
} | |
public class OrderCancelled : IEvent | |
{ | |
public Guid OrderId { get; set; } | |
} | |
public class ProductsReturned : IEvent | |
{ | |
public Guid OrderId { get; set; } | |
} | |
public class OrderAccepted : IEvent | |
{ | |
public Guid OrderId { get; set; } | |
public DateTime TimeOfOrder { get; set; } | |
public float Total { get; set; } | |
public Guid CustomerId {get; set; } | |
} | |
public class AccumulatorData : IContainSagaData | |
{ | |
public Guid Id { get; set; } | |
public string Originator { get; set; } | |
public string OriginalMessageId { get; set; } | |
public float SevenDayTotal { get; set; } | |
[Unique] | |
public Guid CustomerId { get; set; } | |
} | |
public class AccumulatorSaga : Saga<AccumulatorData>, | |
IAmStartedByMessages<OrderAccepted>, | |
IHandleTimeouts<OrderAccepted> | |
{ | |
public void Handle(OrderAccepted message) | |
{ | |
if (Data.SevenDayTotal > 500) ; | |
Data.CustomerId = message.CustomerId; | |
Data.SevenDayTotal += message.Total; | |
RequestUtcTimeout(TimeSpan.FromDays(7), message); | |
} | |
public void Timeout(OrderAccepted state) | |
{ | |
Data.SevenDayTotal -= state.Total; | |
} | |
public override void ConfigureHowToFindSaga() | |
{ | |
ConfigureMapping<OrderAccepted>(s => s.CustomerId, m => m.CustomerId); | |
} | |
} | |
public class SagaCOnfiguration : INeedInitialization | |
{ | |
public void Init() | |
{ | |
Configure.Instance.Configurer | |
.ConfigureProperty<RefundPolicy>(s => s.FirstWindow, TimeSpan.FromDays(30)) | |
.ConfigureProperty<RefundPolicy>(s => s.SecondWindow, TimeSpan.FromDays(60)); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment