Skip to content

Instantly share code, notes, and snippets.

@jasonmitchell
Last active June 5, 2017 11:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jasonmitchell/2be765fc56b0614e0acf2297f2c51b63 to your computer and use it in GitHub Desktop.
Save jasonmitchell/2be765fc56b0614e0acf2297f2c51b63 to your computer and use it in GitHub Desktop.
Event sourced handler
public class RequestReservation { }
public class ConfirmReservation { }
public class ReservationRequested { }
public class ReservationConfirmed { }
public abstract class Handler<TState>
{
protected void Stream(Func<TState, string> action)
{
}
protected void Given<TEvent>(Func<TState, TEvent, TState> given)
{
}
protected void When<TMessage>(Func<TState, TMessage, IEnumerable<object>> when)
{
}
}
public class ReservationHandler : Handler<ReservationState>
{
public ReservationHandler()
{
// How dafuq does this work?
Stream(s => $"Reservation-{s.Id}");
Given<ReservationRequested>((s, e) =>
{
// Mutate state
return s;
});
Given<ReservationConfirmed>((s, e) =>
{
// Mutate state
return s;
});
When<RequestReservation>((s, e) =>
{
// Some business logic stuff
return new[]
{
new ReservationRequested()
};
});
When<ConfirmReservation>((s, e) =>
{
// Some business logic stuff
return new[]
{
new ReservationConfirmed()
};
});
}
}
public class ReservationState
{
public Guid Id { get; }
public string TicketId { get; }
public int Quantity { get; }
public ReservationState(Guid id, string ticketId, int quantity)
{
Id = id;
TicketId = ticketId;
Quantity = quantity;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment