Skip to content

Instantly share code, notes, and snippets.

@dcomartin
Created May 25, 2022
Embed
What would you like to do?
public class CreateShippingLabelSagaData : ContainSagaData
{
public Guid OrderId { get; set; }
public bool IsOrderPlaced { get; set; }
public bool IsOrderBilled { get; set; }
}
public class CreateShippingLabelSaga :
Saga<CreateShippingLabelSagaData>,
IAmStartedByMessages<OrderPlaced>,
IAmStartedByMessages<OrderBilled>
{
protected override void ConfigureHowToFindSaga(SagaPropertyMapper<CreateShippingLabelSagaData> mapper)
{
mapper.ConfigureMapping<OrderPlaced>(message => message.OrderId).ToSaga(sagaData => sagaData.OrderId);
mapper.ConfigureMapping<OrderBilled>(message => message.OrderId).ToSaga(sagaData => sagaData.OrderId);
}
public Task Handle(OrderPlaced message, IMessageHandlerContext context)
{
Data.IsOrderPlaced = true;
return ProcessOrder(context);
}
public Task Handle(OrderBilled message, IMessageHandlerContext context)
{
Data.IsOrderBilled = true;
return ProcessOrder(context);
}
private async Task ProcessOrder(IMessageHandlerContext context)
{
if (Data.IsOrderPlaced && Data.IsOrderBilled)
{
await context.SendLocal(new CreateShippingLabel() { OrderId = Data.OrderId });
MarkAsComplete();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment