예제 #1
0
 // TODO: Put strings into the resources.
 //
 public static MetadataPropertyHandle <TMetadata, TPropertyValue> EnsureValid <TMetadata, TPropertyValue>(this MetadataPropertyHandle <TMetadata, TPropertyValue> hnd, ValueValidator <TPropertyValue> validator)
     where TMetadata : class, IMetadata
 {
     //
     hnd.EnsureHandleValid();
     validator.EnsureNotNull(nameof(validator));
     //
     if (hnd.PropertyValue != null)
     {
         bool   validationResult;
         string validationErrorMessage;
         try {
             validationResult =
                 validator(
                     value: hnd.PropertyValue,
                     errorMessage: out validationErrorMessage);
         }
         catch (Exception firstException) {
             throw
                 new MetadataValidationException(
                     message: $"Ошибка валидации свойства '{hnd.PropertyName}'.",
                     innerException: firstException,
                     metadata: hnd.Metadata);
         }
         if (!validationResult)
         {
             throw
                 new MetadataValidationException(
                     message: $"Валидация свойства '{hnd.PropertyName}' выявила ошибку(-и).{Environment.NewLine}{validationErrorMessage}",
                     metadata: hnd.Metadata);
         }
     }
     //
     return(hnd);
 }