From 41a88416fb604276cbf8af44ac6dcf6ad6511d42 Mon Sep 17 00:00:00 2001 From: jwaliszko Date: Sat, 28 May 2016 15:49:11 +0200 Subject: [PATCH] Use compile-time identifiers instead of magic strings for resource object properties identification. --- .../Models/Address.cs | 4 +- .../Models/Contact.cs | 14 ++-- .../Models/Query.cs | 76 +++++++++---------- .../Views/Home/EditorTemplates/Contact.cshtml | 2 +- 4 files changed, 48 insertions(+), 48 deletions(-) diff --git a/src/ExpressiveAnnotations.MvcWebSample/Models/Address.cs b/src/ExpressiveAnnotations.MvcWebSample/Models/Address.cs index e97c43f..8f68490 100644 --- a/src/ExpressiveAnnotations.MvcWebSample/Models/Address.cs +++ b/src/ExpressiveAnnotations.MvcWebSample/Models/Address.cs @@ -7,9 +7,9 @@ public class Address public string Type { get; set; } [AssertThat("StartsWith(Details, StreetPrefix)", Priority = 1, - ErrorMessageResourceType = typeof (Resources), ErrorMessageResourceName = "AddressDetailsFormatInvalid")] + ErrorMessageResourceType = typeof (Resources), ErrorMessageResourceName = nameof(Resources.AddressDetailsFormatInvalid))] [AssertThat("Length(Trim(Details)) > Length(StreetPrefix) + 1", Priority = 2, - ErrorMessageResourceType = typeof(Resources), ErrorMessageResourceName = "AddressDetailsTooShort")] + ErrorMessageResourceType = typeof (Resources), ErrorMessageResourceName = nameof(Resources.AddressDetailsTooShort))] public string Details { get; set; } public string StreetPrefix => Resources.StreetPrefix; diff --git a/src/ExpressiveAnnotations.MvcWebSample/Models/Contact.cs b/src/ExpressiveAnnotations.MvcWebSample/Models/Contact.cs index b334ccd..e898a3f 100644 --- a/src/ExpressiveAnnotations.MvcWebSample/Models/Contact.cs +++ b/src/ExpressiveAnnotations.MvcWebSample/Models/Contact.cs @@ -12,20 +12,20 @@ public Contact() } [RequiredIf("Phone == null", - ErrorMessageResourceType = typeof (Resources), ErrorMessageResourceName = "EmailOrPhoneRequired")] + ErrorMessageResourceType = typeof (Resources), ErrorMessageResourceName = nameof(Resources.EmailOrPhoneRequired))] [AssertThat("IsEmail(Email)", - ErrorMessageResourceType = typeof (Resources), ErrorMessageResourceName = "EmailFormatInvalid")] - [Display(ResourceType = typeof (Resources), Name = "Email")] + ErrorMessageResourceType = typeof (Resources), ErrorMessageResourceName = nameof(Resources.EmailFormatInvalid))] + [Display(ResourceType = typeof (Resources), Name = nameof(Resources.Email))] public string Email { get; set; } [RequiredIf("Email == null", - ErrorMessageResourceType = typeof (Resources), ErrorMessageResourceName = "EmailOrPhoneRequired")] + ErrorMessageResourceType = typeof (Resources), ErrorMessageResourceName = nameof(Resources.EmailOrPhoneRequired))] [AssertThat(@"IsRegexMatch(Phone, '^\\d+$')", // regex pattern escaped despite verbatim string - it's because our expressive language parser // verbatim syntax should be perfectly valid with that one JavaScript accepts - ErrorMessageResourceType = typeof (Resources), ErrorMessageResourceName = "DigitsOnlyAccepted", Priority = 1)] + ErrorMessageResourceType = typeof (Resources), ErrorMessageResourceName = nameof(Resources.DigitsOnlyAccepted), Priority = 1)] [AssertThat("Length(Phone) > 8 && Length(Phone) < 16", - ErrorMessageResourceType = typeof (Resources), ErrorMessageResourceName = "RangeViolated", Priority = 2)] - [Display(ResourceType = typeof (Resources), Name = "Phone")] + ErrorMessageResourceType = typeof (Resources), ErrorMessageResourceName = nameof(Resources.RangeViolated), Priority = 2)] + [Display(ResourceType = typeof (Resources), Name = nameof(Resources.Phone))] public string Phone { get; set; } public List
Addresses { get; set; } diff --git a/src/ExpressiveAnnotations.MvcWebSample/Models/Query.cs b/src/ExpressiveAnnotations.MvcWebSample/Models/Query.cs index a1c5c86..65ac2b0 100644 --- a/src/ExpressiveAnnotations.MvcWebSample/Models/Query.cs +++ b/src/ExpressiveAnnotations.MvcWebSample/Models/Query.cs @@ -58,27 +58,27 @@ public Query() public TimeSpan WeekPeriod => new TimeSpan(7, 0, 0, 0); - [Display(ResourceType = typeof (Resources), Name = "GoAbroad")] + [Display(ResourceType = typeof (Resources), Name = nameof(Resources.GoAbroad))] public bool GoAbroad { get; set; } - [Required(ErrorMessageResourceType = typeof (Resources), ErrorMessageResourceName = "FieldRequired")] - [Display(ResourceType = typeof (Resources), Name = "Age")] + [Required(ErrorMessageResourceType = typeof (Resources), ErrorMessageResourceName = nameof(Resources.FieldRequired))] + [Display(ResourceType = typeof (Resources), Name = nameof(Resources.Age))] public int? Age { get; set; } [RequiredIf("GoAbroad == true", ErrorMessage = "?")] public string ID { get; set; } [RequiredIf("GoAbroad == true", - ErrorMessageResourceType = typeof (Resources), ErrorMessageResourceName = "FieldConditionallyRequired")] + ErrorMessageResourceType = typeof (Resources), ErrorMessageResourceName = nameof(Resources.FieldConditionallyRequired))] [AssertThat("IsDigitChain(PassportNumber)", - ErrorMessageResourceType = typeof (Resources), ErrorMessageResourceName = "DigitsOnlyAccepted")] - [Display(ResourceType = typeof (Resources), Name = "PassportNumber")] + ErrorMessageResourceType = typeof (Resources), ErrorMessageResourceName = nameof(Resources.DigitsOnlyAccepted))] + [Display(ResourceType = typeof (Resources), Name = nameof(Resources.PassportNumber))] public string PassportNumber { get; set; } - [Display(ResourceType = typeof (Resources), Name = "Country")] + [Display(ResourceType = typeof (Resources), Name = nameof(Resources.Country))] public string Country { get; set; } - [Display(ResourceType = typeof (Resources), Name = "NextCountry")] + [Display(ResourceType = typeof (Resources), Name = nameof(Resources.NextCountry))] public string NextCountry { get; set; } [RequiredIf(@"GoAbroad == true @@ -86,12 +86,12 @@ public Query() (NextCountry != 'Other' && NextCountry == Country) || (Age > 24 && Age <= 55) )", - ErrorMessageResourceType = typeof (Resources), ErrorMessageResourceName = "ReasonForTravelRequired")] + ErrorMessageResourceType = typeof (Resources), ErrorMessageResourceName = nameof(Resources.ReasonForTravelRequired))] [RequiredIf("ArrayContains(Age, EarlyYears)", - ErrorMessageResourceType = typeof (Resources), ErrorMessageResourceName = "ReasonForTravelRequiredForYouth")] + ErrorMessageResourceType = typeof (Resources), ErrorMessageResourceName = nameof(Resources.ReasonForTravelRequiredForYouth))] [AssertThat(@"ReasonForTravel != 'John\'s cat named ""\\\'""\n (Backslash Quote)' && ReasonForTravel != SIMONS_CAT", - ErrorMessageResourceType = typeof (Resources), ErrorMessageResourceName = "SecretAnswerDetected")] - [Display(ResourceType = typeof (Resources), Name = "ReasonForTravel")] + ErrorMessageResourceType = typeof (Resources), ErrorMessageResourceName = nameof(Resources.SecretAnswerDetected))] + [Display(ResourceType = typeof (Resources), Name = nameof(Resources.ReasonForTravel))] public string ReasonForTravel { get; set; } [UIHint("ISO8601Date")] @@ -100,25 +100,25 @@ public Query() public DateTime LatestSuggestedReturnDate { get; set; } [RequiredIf("GoAbroad == true", - ErrorMessageResourceType = typeof (Resources), ErrorMessageResourceName = "FieldConditionallyRequired")] + ErrorMessageResourceType = typeof (Resources), ErrorMessageResourceName = nameof(Resources.FieldConditionallyRequired))] [AssertThat("ReturnDate >= Today()", Priority = 1, // to be invoked firstly - ErrorMessageResourceType = typeof (Resources), ErrorMessageResourceName = "FutureDateRequired")] + ErrorMessageResourceType = typeof (Resources), ErrorMessageResourceName = nameof(Resources.FutureDateRequired))] [AssertThat("ReturnDate >= Today() + WeekPeriod", Priority = 2, // to be invoked secondly - ErrorMessageResourceType = typeof (Resources), ErrorMessageResourceName = "MoreThanAWeekRequired")] + ErrorMessageResourceType = typeof (Resources), ErrorMessageResourceName = nameof(Resources.MoreThanAWeekRequired))] [AssertThat("ReturnDate < AddYears(Today(), 1)", - ErrorMessageResourceType = typeof (Resources), ErrorMessageResourceName = "NoMoreThanAYear")] - [Display(ResourceType = typeof (Resources), Name = "ReturnDate")] + ErrorMessageResourceType = typeof (Resources), ErrorMessageResourceName = nameof(Resources.NoMoreThanAYear))] + [Display(ResourceType = typeof (Resources), Name = nameof(Resources.ReturnDate))] [ValueParser("NonStandardDateParser")] public DateTime? ReturnDate { get; set; } [RequiredIf("GoAbroad == true && ReturnDate > LatestSuggestedReturnDate", AllowEmptyStrings = true, - ErrorMessageResourceType = typeof (Resources), ErrorMessageResourceName = "ReasonForLongTravelRequired")] - [Display(ResourceType = typeof (Resources), Name = "ReasonForLongTravel")] + ErrorMessageResourceType = typeof (Resources), ErrorMessageResourceName = nameof(Resources.ReasonForLongTravelRequired))] + [Display(ResourceType = typeof (Resources), Name = nameof(Resources.ReasonForLongTravel))] public string ReasonForLongTravel { get; set; } [RequiredIf("GoAbroad == true", - ErrorMessageResourceType = typeof (Resources), ErrorMessageResourceName = "FieldConditionallyRequired")] - [Display(ResourceType = typeof (Resources), Name = "PoliticalStability")] + ErrorMessageResourceType = typeof (Resources), ErrorMessageResourceName = nameof(Resources.FieldConditionallyRequired))] + [Display(ResourceType = typeof (Resources), Name = nameof(Resources.PoliticalStability))] public Stability? PoliticalStability { get; set; } [AssertThat(@"( @@ -126,52 +126,52 @@ public Query() && (PoliticalStability == Stability.Low || PoliticalStability == Stability.Uncertain) ) || PoliticalStability == null || PoliticalStability == Stability.High", - ErrorMessageResourceType = typeof (Resources), ErrorMessageResourceName = "AwareOfTheRisksRequired")] - [Display(ResourceType = typeof (Resources), Name = "AwareOfTheRisks")] + ErrorMessageResourceType = typeof (Resources), ErrorMessageResourceName = nameof(Resources.AwareOfTheRisksRequired))] + [Display(ResourceType = typeof (Resources), Name = nameof(Resources.AwareOfTheRisks))] public bool AwareOfTheRisks { get; set; } - [Display(ResourceType = typeof (Resources), Name = "SportType")] + [Display(ResourceType = typeof (Resources), Name = nameof(Resources.SportType))] public string SportType { get; set; } [RequiredIf("SportType == 'Extreme' || (SportType != 'None' && GoAbroad == true)", - ErrorMessageResourceType = typeof (Resources), ErrorMessageResourceName = "BloodTypeRequired")] + ErrorMessageResourceType = typeof (Resources), ErrorMessageResourceName = nameof(Resources.BloodTypeRequired))] [AssertThat("IsBloodType(Trim(BloodType))", - ErrorMessageResourceType = typeof (Resources), ErrorMessageResourceName = "BloodTypeInvalid")] - [Display(ResourceType = typeof (Resources), Name = "BloodType")] + ErrorMessageResourceType = typeof (Resources), ErrorMessageResourceName = nameof(Resources.BloodTypeInvalid))] + [Display(ResourceType = typeof (Resources), Name = nameof(Resources.BloodType))] public string BloodType { get; set; } [AssertThat("AgreeForContact == true", - ErrorMessageResourceType = typeof (Resources), ErrorMessageResourceName = "AgreeForContactRequired")] - [Display(ResourceType = typeof (Resources), Name = "AgreeForContact")] + ErrorMessageResourceType = typeof (Resources), ErrorMessageResourceName = nameof(Resources.AgreeForContactRequired))] + [Display(ResourceType = typeof (Resources), Name = nameof(Resources.AgreeForContact))] public bool AgreeForContact { get; set; } [RequiredIf(@"AgreeForContact == true && (ContactDetails.Email != null || ContactDetails.Phone != null) && (ContactDetails.Addresses[0].Details != null || ContactDetails.Addresses[1].Details != null)", - ErrorMessageResourceType = typeof (Resources), ErrorMessageResourceName = "ImmediateContactRequired")] - [Display(ResourceType = typeof (Resources), Name = "ImmediateContact")] + ErrorMessageResourceType = typeof (Resources), ErrorMessageResourceName = nameof(Resources.ImmediateContactRequired))] + [Display(ResourceType = typeof (Resources), Name = nameof(Resources.ImmediateContact))] public bool? ImmediateContact { get; set; } [AssertThat(@"FlightId != Guid('00000000-0000-0000-0000-000000000000') || !GoAbroad", - ErrorMessageResourceType = typeof (Resources), ErrorMessageResourceName = "FlightIdentifierInvalid")] - [Display(ResourceType = typeof (Resources), Name = "FlightId")] + ErrorMessageResourceType = typeof (Resources), ErrorMessageResourceName = nameof(Resources.FlightIdentifierInvalid))] + [Display(ResourceType = typeof (Resources), Name = nameof(Resources.FlightId))] public Guid FlightId { get; set; } [RequiredIf("GoAbroad == true", - ErrorMessageResourceType = typeof (Resources), ErrorMessageResourceName = "FieldConditionallyRequired")] + ErrorMessageResourceType = typeof (Resources), ErrorMessageResourceName = nameof(Resources.FieldConditionallyRequired))] [AssertThat(@"GoAbroad ? ArrayLength(SelectedDonations) > 2 : ArrayLength(SelectedDonations) > 1", - ErrorMessageResourceType = typeof (Resources), ErrorMessageResourceName = "NotEnoughDonations")] + ErrorMessageResourceType = typeof (Resources), ErrorMessageResourceName = nameof(Resources.NotEnoughDonations))] [ValueParser("ArrayParser")] - [Display(ResourceType = typeof (Resources), Name = "Donation")] + [Display(ResourceType = typeof (Resources), Name = nameof(Resources.Donation))] public int[] SelectedDonations { get; set; } [CustomRequiredIf("GoAbroad == true")] [CustomAssertThat("Length(コメント) > 1e1 - 1", Priority = 2)] [CustomAssertThat("Length(コメント) > 1e1 - 6", Priority = 1, - ErrorMessageResourceType = typeof (Resources), ErrorMessageResourceName = "JustAFewCharsMore")] - [Display(ResourceType = typeof (Resources), Name = "Comment")] + ErrorMessageResourceType = typeof (Resources), ErrorMessageResourceName = nameof(Resources.JustAFewCharsMore))] + [Display(ResourceType = typeof (Resources), Name = nameof(Resources.Comment))] public string コメント { get; set; } public Contact ContactDetails { get; set; } diff --git a/src/ExpressiveAnnotations.MvcWebSample/Views/Home/EditorTemplates/Contact.cshtml b/src/ExpressiveAnnotations.MvcWebSample/Views/Home/EditorTemplates/Contact.cshtml index e5c7310..84d0b7e 100644 --- a/src/ExpressiveAnnotations.MvcWebSample/Views/Home/EditorTemplates/Contact.cshtml +++ b/src/ExpressiveAnnotations.MvcWebSample/Views/Home/EditorTemplates/Contact.cshtml @@ -21,7 +21,7 @@
[show attribute]
- @Html.LabelFor(model => model.Phone, new {@class = "inline prefix"}) + @Html.LabelFor(model => model.Phone, new { @class = "inline prefix" }) @Html.TextBoxFor(model => model.Phone) @Html.ValidationMessageFor(model => model.Phone)