Beispiel #1
0
        public static CommandSettings CreateSettings(CommandValueLookup lookup, Type settingsType, ITypeResolver resolver)
        {
            var settings = CreateSettings(resolver, settingsType);

            foreach (var(parameter, value) in lookup)
            {
                parameter.Property.SetValue(settings, value);
            }

            // Validate the settings.
            var validationResult = settings.Validate();

            if (!validationResult.Successful)
            {
                throw CommandRuntimeException.ValidationFailed(validationResult);
            }

            return(settings);
        }
Beispiel #2
0
        public static void ValidateParameter(CommandParameter parameter, CommandValueLookup settings)
        {
            var assignedValue = settings.GetValue(parameter);

            foreach (var validator in parameter.Validators)
            {
                var validationResult = validator.Validate(parameter, assignedValue);
                if (!validationResult.Successful)
                {
                    // If there is a error message specified in the parameter validator attribute,
                    // then use that one, otherwise use the validation result.
                    var result = string.IsNullOrWhiteSpace(validator.ErrorMessage)
                        ? validationResult
                        : ValidationResult.Error(validator.ErrorMessage);

                    throw CommandRuntimeException.ValidationFailed(result);
                }
            }
        }
        private static Task <int> Execute(
            CommandTree leaf,
            CommandTree tree,
            CommandContext context,
            ITypeResolver resolver,
            IConfiguration configuration)
        {
            // Bind the command tree against the settings.
            var settings = CommandBinder.Bind(tree, leaf.Command.SettingsType, resolver);

            configuration.Settings.Interceptor?.Intercept(context, settings);

            // Create and validate the command.
            var command          = leaf.CreateCommand(resolver);
            var validationResult = command.Validate(context, settings);

            if (!validationResult.Successful)
            {
                throw CommandRuntimeException.ValidationFailed(validationResult);
            }

            // Execute the command.
            return(command.Execute(context, settings));
        }