public TContext ProcessCommand(TContext context, CommandEvent commandEvent) { // Parse TournamentCommand commandName; if (!Enum.TryParse <TournamentCommand>(commandEvent.Name, out commandName)) { throw new Exception(String.Format("Unknown command name: {0}", commandEvent.Name)); } // Dispatch switch (commandName) { case TournamentCommand.CreateTournament: return(CreateTournament(context, commandEvent)); case TournamentCommand.AddPlayer: return(AddPlayer(context, commandEvent)); case TournamentCommand.RemovePlayer: return(RemovePlayer(context, commandEvent)); case TournamentCommand.RecordMatchResults: return(RecordMatchResults(context, commandEvent)); case TournamentCommand.EndRound: return(EndRound(context, commandEvent)); default: throw new Exception(String.Format("Unhandled TournamentCommand: {0}", commandName)); } }
TContext EndRound(TContext context, CommandEvent commandEvent) { var command = new EndRoundCommand(); return(Fire( context, TournamentCommand.EndRound, () => ContextBuilder.ProcessCommand(context, command) )); }
TContext RemovePlayer(TContext context, CommandEvent commandEvent) { var player = new Player(commandEvent.Properties["Player"], null); var command = new RemovePlayerCommand(player); return(Fire( context, TournamentCommand.RemovePlayer, () => ContextBuilder.ProcessCommand(context, command) )); }
TContext CreateTournament(TContext context, CommandEvent commandEvent) { var timestamp = DateTimeOffset.Parse(commandEvent.Properties["Timestamp"]); var command = new CreateTournamentCommand(timestamp); return(Fire( context, TournamentCommand.CreateTournament, () => ContextBuilder.ProcessCommand(context, command) )); }
TContext RecordMatchResults(TContext context, CommandEvent commandEvent) { var player = new Player(commandEvent.Properties["Player"], null); int winsValue; int?wins = null; if (commandEvent.Properties.ContainsKey("Wins") && Int32.TryParse(commandEvent.Properties["Wins"], out winsValue)) { wins = winsValue; } int lossesValue; int?losses = null; if (commandEvent.Properties.ContainsKey("Losses") && Int32.TryParse(commandEvent.Properties["Losses"], out lossesValue)) { losses = lossesValue; } int drawsValue; int?draws = null; if (commandEvent.Properties.ContainsKey("Draws") && Int32.TryParse(commandEvent.Properties["Draws"], out drawsValue)) { draws = drawsValue; } var command = new RecordMatchResultsCommand(player, wins, losses, draws); return(Fire( context, TournamentCommand.RecordMatchResults, () => ContextBuilder.ProcessCommand(context, command) )); }