Qual è lo scopo di suffissi .RequiredError, .FormatError, ecc
-
13-09-2019 - |
Domanda
La lettura del codice sorgente Oxite, ho trovato che i validatori risparmiare nome di proprietà male con alcuni suffissi (RequiredError, MaxLengthExceededError, InvalidError, FormatError)
validationState.Errors.Add(CreateValidationError(user.Name, "Name.RequiredError", "Name is not set"));
validationState.Errors.Add(CreateValidationError(user.Name, "Name.MaxLengthExceededError", "Username must be less than or equal to {0} characters long.", 256));
validationState.Errors.Add(CreateValidationError(user.Email, "Email.InvalidError", "Email is invalid."));
Qual è lo scopo di cui suffissi? Come hanno usato?
Soluzione
La mia ipotesi è che sono valori costanti, è adatto alle macchine che possono essere utilizzati per identificare in modo univoco l'errore e può essere utilizzato per recuperare risorse localizzate per il tuo sito globalizzato.
Sono un buon indovino:
protected ValidationError CreateValidationError(
object value, string validationKey, string validationMessage,
params object[] validationMessageParameters)
{
if (validationMessageParameters != null &&
validationMessageParameters.Length > 0)
{
validationMessage = string.Format(
validationMessage, validationMessageParameters);
}
return new ValidationError(
validationKey,
value,
new InvalidOperationException(
localize(validationKey, validationMessage))
);
}
private string localize(string key, string defaultValue)
{
if (phrases == null)
phrases = localizationService.GetTranslations();
Phrase foundPhrase = phrases
.Where(p => p.Key == key && p.Language == site.LanguageDefault)
.FirstOrDefault();
if (foundPhrase != null)
return foundPhrase.Value;
return defaultValue;
}
Curioso, però. Dal momento che le eccezioni in genere non dovrebbero essere localizzati.
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow