public IEnumerable <IEvent> HandleCreateScorecard(CreateScorecard command)
        {
            // Validate the command.
            if (State != null)
            {
                return(new IEvent[] { new Failed("Can't create an existing scorecard.") });
            }
            if (command == null)
            {
                return(new IEvent[] { new Failed("Can handle a null command.") });
            }
            var validationResult = ScorecardValidator.Validate(command);

            if (!validationResult.IsValid)
            {
                return(new IEvent[] { new Failed(GetFormattedErrorMessage(validationResult)) });
            }

            return(new IEvent[] { new ScorecardCreated(command) });
        }
        public IEnumerable <IEvent> HandleUpdateScorecard(UpdateScorecard command)
        {
            // Validate the command.
            if (State == null)
            {
                return(new IEvent[] { new Failed("Scorecard doesn't exist.") });
            }
            if (Deleted)
            {
                return(new IEvent[] { new Failed("The scorecard was previously deleted.") });
            }
            var validationResult = ScorecardValidator.Validate(command);

            if (!validationResult.IsValid)
            {
                return(new IEvent[] { new Failed(GetFormattedErrorMessage(validationResult)) });
            }

            // Validate that the command doesn't conflict with the current state.
            if (command.Id != State.Id)
            {
                validationResult.Errors.Add(new ValidationFailure("Id", "Id mismatch."));
            }
            if (command.Scores.Length != State.Scores.Length)
            {
                validationResult.Errors.Add(new ValidationFailure("HolePars", "Cannot change the number of holes."));
            }
            if (command.PlayerId != State.PlayerId)
            {
                validationResult.Errors.Add(new ValidationFailure("PlayerId", "Cannot change a scorecard's player."));
            }
            if (!validationResult.IsValid)
            {
                return(new IEvent[] { new Failed(GetFormattedErrorMessage(validationResult)) });
            }

            return(new IEvent[] { new ScorecardUpdated(command) });
        }