public async Task <JsonResult> RunCommandAsync <TCommand>(TCommand command) where TCommand : ICommand { var errors = _commandValidationService.GetErrors(command).ToList(); if (!errors.Any()) { try { await _commandExecutor.ExecuteAsync(command); } catch (ValidationException ex) { AddValidationExceptionToErrorList(ex, errors); } catch (NotPermittedException ex) { return(NotPermittedResponse(ex)); } } var outputValue = GetCommandOutputValue(command); if (outputValue != null) { return(SimpleCommandResponse(errors, outputValue)); } return(SimpleCommandResponse(errors)); }
/// <summary> /// Creates an instance of a settings objects, extracting setting values from /// a configuration source. /// </summary> public TSettings Create() { var settings = new TSettings(); var objType = typeof(TSettings); var properties = objType.GetProperties(BindingFlags.Public | BindingFlags.Instance);; foreach (var property in properties) { var name = GetPropertyName(settings, property); var stringValue = _configurationService.GetSettingOrDefault(name, null); if (!string.IsNullOrWhiteSpace(stringValue)) { var typedValue = ConvertValue(property, stringValue, name); property.SetValue(settings, typedValue, null); } } var errors = _modelValidationService.GetErrors(settings); if (!EnumerableHelper.IsNullOrEmpty(errors)) { throw new InvalidConfigurationException(objType.Name, errors); } return(settings); }
/// <summary> /// Because model binding isn't supported in view components, we have to /// manually validate the model. /// </summary> private void ValidateModel <TModel>(TModel model) { var errors = _modelValidationService.GetErrors(model); foreach (var error in errors) { ModelState.AddModelError(error.Properties.FirstOrDefault(), error.Message); } }
public JsonResult Validate([ModelBinder(BinderType = typeof(NestedDataModelMultiTypeItemModelBinder))] NestedDataModelMultiTypeItem item) { if (item?.Model == null) { throw new Exception("Error binding model"); } var errors = _modelValidationService.GetErrors(item.Model).ToList(); return(_apiResponseHelper.SimpleCommandResponse(errors)); }
/// <summary> /// Creates an instance of a settings objects, extracting setting values from /// a configuration source. /// </summary> public TSettings Create() { var settingsOptions = _serviceProvider.GetRequiredService <IOptions <TSettings> >(); var settings = settingsOptions.Value; var errors = _modelValidationService.GetErrors(settings); if (!EnumerableHelper.IsNullOrEmpty(errors)) { throw new InvalidConfigurationException(typeof(TSettings), errors); } return(settings); }
/// <summary> /// Creates an instance of a settings objects, extracting setting values from /// a configuration source. /// </summary> public TSettings Create() { var settingsOptions = _serviceProvider.GetRequiredService <IOptions <TSettings> >(); var settings = settingsOptions.Value; if (settings is IFeatureEnableable featureEnableable && !featureEnableable.Enabled) { // feature is disabled, so skip validation. return(settings); } var errors = _modelValidationService.GetErrors(settings); if (!EnumerableHelper.IsNullOrEmpty(errors)) { throw new InvalidConfigurationException(typeof(TSettings), errors); } return(settings); }