From 2528710897c6995eaa6b04a63c1daa8cdffbf29d Mon Sep 17 00:00:00 2001 From: 杨宇千 Date: Fri, 25 Oct 2019 18:36:02 +0800 Subject: Add NeutralResourcesLanguage. Conform to best practices. --- Timeline/Models/Validation/Validator.cs | 41 ++++++++++----------------------- 1 file changed, 12 insertions(+), 29 deletions(-) (limited to 'Timeline/Models/Validation/Validator.cs') diff --git a/Timeline/Models/Validation/Validator.cs b/Timeline/Models/Validation/Validator.cs index d2c7c377..a16f6f81 100644 --- a/Timeline/Models/Validation/Validator.cs +++ b/Timeline/Models/Validation/Validator.cs @@ -3,17 +3,10 @@ using Microsoft.Extensions.Localization; using System; using System.ComponentModel.DataAnnotations; using Timeline.Helpers; +using static Timeline.Resources.Models.Validation.Validator; namespace Timeline.Models.Validation { - /// - /// Generate a message from a localizer factory. - /// If localizerFactory is null, it should return a culture-invariant message. - /// - /// The localizer factory. Could be null. - /// The message. - public delegate string ValidationMessageGenerator(IStringLocalizerFactory? localizerFactory); - /// /// A validator to validate value. /// @@ -23,8 +16,8 @@ namespace Timeline.Models.Validation /// Validate given value. /// /// The value to validate. - /// Validation success or not and the message generator. - (bool, ValidationMessageGenerator) Validate(object? value); + /// Validation success or not and message. + (bool, string) Validate(object? value); } /// @@ -40,14 +33,11 @@ namespace Timeline.Models.Validation /// public abstract class Validator : IValidator { - public (bool, ValidationMessageGenerator) Validate(object? value) + public (bool, string) Validate(object? value) { if (value == null) { - return (false, factory => - factory?.Create("Models.Validation.Validator")?["ValidatorMessageNull"] - ?? Resources.Models.Validation.Validator.InvariantValidatorMessageNull - ); + return (false, ValidatorMessageNull); } if (value is T v) @@ -56,16 +46,13 @@ namespace Timeline.Models.Validation } else { - return (false, factory => - factory?.Create("Models.Validation.Validator")?["ValidatorMessageBadType", typeof(T).FullName] - ?? Resources.Models.Validation.Validator.InvariantValidatorMessageBadType); + return (false, ValidatorMessageBadType); } } - protected static ValidationMessageGenerator SuccessMessageGenerator { get; } = factory => - factory?.Create("Models.Validation.Validator")?["ValidatorMessageSuccess"] ?? Resources.Models.Validation.Validator.InvariantValidatorMessageSuccess; + protected static string GetSuccessMessage() => ValidatorMessageSuccess; - protected abstract (bool, ValidationMessageGenerator) DoValidate(T value); + protected abstract (bool, string) DoValidate(T value); } [AttributeUsage(AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Parameter, @@ -93,9 +80,7 @@ namespace Timeline.Models.Validation throw new ArgumentNullException(nameof(validatorType)); if (!typeof(IValidator).IsAssignableFrom(validatorType)) - throw new ArgumentException( - Resources.Models.Validation.Validator.ValidateWithAttributeNotValidator, - nameof(validatorType)); + throw new ArgumentException(ValidateWithAttributeExceptionNotValidator, nameof(validatorType)); try { @@ -103,22 +88,20 @@ namespace Timeline.Models.Validation } catch (Exception e) { - throw new ArgumentException( - Resources.Models.Validation.Validator.ValidateWithAttributeCreateFail, e); + throw new ArgumentException(ValidateWithAttributeExceptionCreateFail, e); } } protected override ValidationResult IsValid(object value, ValidationContext validationContext) { - var (result, messageGenerator) = _validator.Validate(value); + var (result, message) = _validator.Validate(value); if (result) { return ValidationResult.Success; } else { - var localizerFactory = validationContext.GetRequiredService(); - return new ValidationResult(messageGenerator(localizerFactory)); + return new ValidationResult(message); } } } -- cgit v1.2.3