Last active
June 5, 2017 11:49
-
-
Save jasonmitchell/2be765fc56b0614e0acf2297f2c51b63 to your computer and use it in GitHub Desktop.
Event sourced handler
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
public class RequestReservation { } | |
public class ConfirmReservation { } |
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
public class ReservationRequested { } | |
public class ReservationConfirmed { } |
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
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) | |
{ | |
} | |
} |
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
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() | |
}; | |
}); | |
} | |
} |
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
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