private static async Task Execute( InMemoryEventStore store, string streamName, Func <EventData[], IEvent[]> f, Func <IEvent[], Task> pub) { var r = await store.LoadEventStreamAsync(streamName, 0); var events = f(r.Events.ToArray()); _ = await store.AppendToStreamAsync(streamName, r.Version, events); await pub(events); }
public async Task Dispatch(ICommand command) { async Task subsciption(IEvent[] events) { _ = await store.AppendToStreamAsync("games", events); await pub(events); }; Func <InMemoryEventStore, Task> commandHandler = command switch { CreateGame cmd => (store) => Execute(store, $"game-{cmd.GameId}", events => Game.Handle(cmd, events).ToArray(), subsciption), JoinGame cmd => (store) => Execute <GameState>(store, $"game-{cmd.GameId}", state => Game.Handle(cmd, state).ToArray(), subsciption), _ => (store) => throw new ArgumentException($"Could not dispatch {command.GetType()}") }; await commandHandler(store); }