Skip to content

Instantly share code, notes, and snippets.

@dcomartin
Created March 30, 2022 21:32
Show Gist options
  • Save dcomartin/e5c78b9699df1c9955ba5add3159a21a to your computer and use it in GitHub Desktop.
Save dcomartin/e5c78b9699df1c9955ba5add3159a21a to your computer and use it in GitHub Desktop.
public class ReserveUsername : ICommand
{
public string Username { get; set; }
}
public class ReserveUsernameHandler : IHandleMessages<ReserveUsername>
{
private readonly UsernameReservation _usernameReservation;
public ReserveUsernameHandler(UsernameReservation usernameReservation)
{
_usernameReservation = usernameReservation;
}
public async Task Handle(ReserveUsername message, IMessageHandlerContext context)
{
Console.WriteLine($"Async: Reserving Username for {message.Username}");
if (_usernameReservation.Reserve(message.Username))
{
var expireOptions = new SendOptions();
expireOptions.DelayDeliveryWith(TimeSpan.FromSeconds(10));
await context.Send(new ExpireReservation { Username = message.Username }, expireOptions);
await context.Publish(new UsernameReserved { Username = message.Username });
}
}
}
public class UsernameReserved : IEvent
{
public string Username { get; set; }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment