Skip to content

Instantly share code, notes, and snippets.

@n1ckdm
Created July 11, 2022 20:50
Show Gist options
  • Save n1ckdm/54146b9560eee52267baae57b66f6849 to your computer and use it in GitHub Desktop.
Save n1ckdm/54146b9560eee52267baae57b66f6849 to your computer and use it in GitHub Desktop.
private async Task<Game> GetCurrentState(Guid gameId) =>
(await _repository.Load(gameId)).Aggregate(new Game(), (g, e) => Game.Apply(g, e));
private List<IEvent> SendToAggregate(ICommand command, Game game) =>
command switch
{
CreateGameCommand c => game.Handle(c),
MakeMoveCommand c => game.Handle(c),
_ => throw new NotImplementedException()
};
public async Task Handle(ICommand cmd)
{
Game game = await GetCurrentState(cmd.GameId);
List<IEvent> events = SendToAggregate(cmd, game);
await _repository.Save(cmd.GameId, events);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment