Skip to content

Instantly share code, notes, and snippets.

@dcomartin
Created March 30, 2022 21:31
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save dcomartin/5a5d3e5a402ce78447924f0a89c62224 to your computer and use it in GitHub Desktop.
public class UserRegistration :
Saga<EmailReservationSagaData>,
IAmStartedByMessages<UserRegistrationStarted>,
IHandleMessages<UsernameReserved>,
IHandleMessages<UserAccountCreated>,
IHandleMessages<UsernameRegistered>
{
protected override void ConfigureHowToFindSaga(SagaPropertyMapper<EmailReservationSagaData> mapper)
{
mapper.ConfigureMapping<UserRegistrationStarted>(message => message.Username).ToSaga(sagaData => sagaData.Username);
mapper.ConfigureMapping<UsernameReserved>(message => message.Username).ToSaga(sagaData => sagaData.Username);
mapper.ConfigureMapping<UserAccountCreated>(message => message.Username).ToSaga(sagaData => sagaData.Username);
mapper.ConfigureMapping<UsernameRegistered>(message => message.Username).ToSaga(sagaData => sagaData.Username);
}
public async Task Handle(UserRegistrationStarted message, IMessageHandlerContext context)
{
await context.Send(new ReserveUsername { Username = message.Username });
}
public async Task Handle(UsernameReserved message, IMessageHandlerContext context)
{
await context.Send(new CreateUserAccount { Username = message.Username });
}
public async Task Handle(UserAccountCreated message, IMessageHandlerContext context)
{
if (message.Username == "test" && Data.Attempts == 0)
{
Data.Attempts++;
Console.WriteLine("Async: Account Creation Failed.");
return;
}
await context.Send(new ConfirmUsernameReservation { Username = message.Username });
}
public Task Handle(UsernameRegistered message, IMessageHandlerContext context)
{
Console.WriteLine($"Async: Registration Complete for {message.Username}");
MarkAsComplete();
return Task.CompletedTask;
}
}
public class EmailReservationSagaData : ContainSagaData
{
public string Username { get; set; }
public int Attempts { get; set; }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment