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/Http/Common.cs | 61 ++++++++++--------------- Timeline/Models/Validation/UsernameValidator.cs | 17 +++---- Timeline/Models/Validation/Validator.cs | 41 +++++------------ 3 files changed, 43 insertions(+), 76 deletions(-) (limited to 'Timeline/Models') diff --git a/Timeline/Models/Http/Common.cs b/Timeline/Models/Http/Common.cs index 39ddddd9..2c9ee9e7 100644 --- a/Timeline/Models/Http/Common.cs +++ b/Timeline/Models/Http/Common.cs @@ -1,5 +1,5 @@ -using Microsoft.Extensions.Localization; -using Timeline.Helpers; +using System.Globalization; +using static Timeline.Resources.Models.Http.Common; namespace Timeline.Models.Http { @@ -27,48 +27,42 @@ namespace Timeline.Models.Http internal static class HeaderErrorResponse { - internal static CommonResponse MissingContentType(IStringLocalizerFactory localizerFactory) + internal static CommonResponse MissingContentType() { - var localizer = localizerFactory.Create("Models.Http.Common"); - return new CommonResponse(ErrorCodes.Http.Common.Header.Missing_ContentType, localizer["HeaderMissingContentType"]); + return new CommonResponse(ErrorCodes.Http.Common.Header.Missing_ContentType, MessageHeaderMissingContentType); } - internal static CommonResponse MissingContentLength(IStringLocalizerFactory localizerFactory) + internal static CommonResponse MissingContentLength() { - var localizer = localizerFactory.Create("Models.Http.Common"); - return new CommonResponse(ErrorCodes.Http.Common.Header.Missing_ContentLength, localizer["HeaderMissingContentLength"]); + return new CommonResponse(ErrorCodes.Http.Common.Header.Missing_ContentLength, MessageHeaderMissingContentLength); } - internal static CommonResponse ZeroContentLength(IStringLocalizerFactory localizerFactory) + internal static CommonResponse ZeroContentLength() { - var localizer = localizerFactory.Create("Models.Http.Common"); - return new CommonResponse(ErrorCodes.Http.Common.Header.Zero_ContentLength, localizer["HeaderZeroContentLength"]); + return new CommonResponse(ErrorCodes.Http.Common.Header.Zero_ContentLength, MessageHeaderZeroContentLength); } - internal static CommonResponse BadIfNonMatch(IStringLocalizerFactory localizerFactory) + internal static CommonResponse BadIfNonMatch() { - var localizer = localizerFactory.Create("Models.Http.Common"); - return new CommonResponse(ErrorCodes.Http.Common.Header.BadFormat_IfNonMatch, localizer["HeaderBadIfNonMatch"]); + return new CommonResponse(ErrorCodes.Http.Common.Header.BadFormat_IfNonMatch, MessageHeaderBadIfNonMatch); } } internal static class ContentErrorResponse { - internal static CommonResponse TooBig(IStringLocalizerFactory localizerFactory, string maxLength) + internal static CommonResponse TooBig(string maxLength) { - var localizer = localizerFactory.Create("Models.Http.Common"); - return new CommonResponse(ErrorCodes.Http.Common.Content.TooBig, localizer["ContentTooBig", maxLength]); + return new CommonResponse(ErrorCodes.Http.Common.Content.TooBig, + string.Format(CultureInfo.CurrentCulture, MessageContentTooBig, maxLength)); } - internal static CommonResponse UnmatchedLength_Smaller(IStringLocalizerFactory localizerFactory) + internal static CommonResponse UnmatchedLength_Smaller() { - var localizer = localizerFactory.Create("Models.Http.Common"); - return new CommonResponse(ErrorCodes.Http.Common.Content.UnmatchedLength_Smaller, localizer["ContentUnmatchedLengthSmaller"]); + return new CommonResponse(ErrorCodes.Http.Common.Content.UnmatchedLength_Smaller, MessageContentUnmatchedLengthSmaller); } - internal static CommonResponse UnmatchedLength_Bigger(IStringLocalizerFactory localizerFactory) + internal static CommonResponse UnmatchedLength_Bigger() { - var localizer = localizerFactory.Create("Models.Http.Common"); - return new CommonResponse(ErrorCodes.Http.Common.Content.UnmatchedLength_Bigger, localizer["ContentUnmatchedLengthBigger"]); + return new CommonResponse(ErrorCodes.Http.Common.Content.UnmatchedLength_Bigger, MessageContentUnmatchedLengthBigger); } } @@ -112,17 +106,14 @@ namespace Timeline.Models.Http } - internal static CommonPutResponse Create(IStringLocalizerFactory localizerFactory) + internal static CommonPutResponse Create() { - var localizer = localizerFactory.Create("Models.Http.Common"); - return new CommonPutResponse(0, localizer["PutCreate"], true); + return new CommonPutResponse(0, MessagePutCreate, true); } - internal static CommonPutResponse Modify(IStringLocalizerFactory localizerFactory) + internal static CommonPutResponse Modify() { - var localizer = localizerFactory.Create("Models.Http.Common"); - return new CommonPutResponse(0, localizer["PutModify"], false); - + return new CommonPutResponse(0, MessagePutModify, false); } } @@ -149,16 +140,14 @@ namespace Timeline.Models.Http } - internal static CommonDeleteResponse Delete(IStringLocalizerFactory localizerFactory) + internal static CommonDeleteResponse Delete() { - var localizer = localizerFactory.Create("Models.Http.Common"); - return new CommonDeleteResponse(0, localizer["DeleteDelete"], true); + return new CommonDeleteResponse(0, MessageDeleteDelete, true); } - internal static CommonDeleteResponse NotExist(IStringLocalizerFactory localizerFactory) + internal static CommonDeleteResponse NotExist() { - var localizer = localizerFactory.Create("Models.Models.Http.Common"); - return new CommonDeleteResponse(0, localizer["DeleteNotExist"], false); + return new CommonDeleteResponse(0, MessageDeleteNotExist, false); } } } diff --git a/Timeline/Models/Validation/UsernameValidator.cs b/Timeline/Models/Validation/UsernameValidator.cs index dc237add..fc6cdf37 100644 --- a/Timeline/Models/Validation/UsernameValidator.cs +++ b/Timeline/Models/Validation/UsernameValidator.cs @@ -1,5 +1,6 @@ using System; using System.Linq; +using static Timeline.Resources.Models.Validation.UsernameValidator; namespace Timeline.Models.Validation { @@ -8,33 +9,27 @@ namespace Timeline.Models.Validation public const int MaxLength = 26; [System.Diagnostics.CodeAnalysis.SuppressMessage("Design", "CA1062:Validate arguments of public methods", Justification = "Already checked in base class.")] - protected override (bool, ValidationMessageGenerator) DoValidate(string value) + protected override (bool, string) DoValidate(string value) { if (value.Length == 0) { - return (false, factory => - factory?.Create(typeof(UsernameValidator))?["ValidationMessageEmptyString"] - ?? Resources.Models.Validation.UsernameValidator.InvariantValidationMessageEmptyString); + return (false, MessageEmptyString); } if (value.Length > 26) { - return (false, factory => - factory?.Create(typeof(UsernameValidator))?["ValidationMessageTooLong"] - ?? Resources.Models.Validation.UsernameValidator.InvariantValidationMessageTooLong); + return (false, MessageTooLong); } foreach ((char c, int i) in value.Select((c, i) => (c, i))) { if (!(char.IsLetterOrDigit(c) || c == '-' || c == '_')) { - return (false, factory => - factory?.Create(typeof(UsernameValidator))?["ValidationMessageInvalidChar"] - ?? Resources.Models.Validation.UsernameValidator.InvariantValidationMessageInvalidChar); + return (false, MessageInvalidChar); } } - return (true, SuccessMessageGenerator); + return (true, GetSuccessMessage()); } } 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