예제 #1
0
 public static IEnumerable <IEvent> Handle(CreateGame command, params IEvent[] events)
 => new[] {
     new GameCreated
     {
         GameId   = command.GameId,
         PlayerId = command.PlayerId,
         Title    = command.Title,
         Rounds   = command.Rounds,
         Created  = DateTime.UtcNow,
         Status   = GameStatus.ReadyToStart
     }
 };
예제 #2
0
        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);
        }