Created
April 30, 2016 16:28
-
-
Save kdowswell/ed7f9adeb1f03b849e899ad9a9a14539 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public abstract class MessageValidator<IRequest> | |
{ | |
private readonly DataAnnotationsValidator _dataAnnotationsValidator; | |
protected ValidationResult _result; | |
protected MessageValidator() | |
{ | |
_dataAnnotationsValidator = new DataAnnotationsValidator(); | |
_result = new ValidationResult(); | |
} | |
public bool ValidateAnnotations(IRequest message) | |
{ | |
List<System.ComponentModel.DataAnnotations.ValidationResult> dataAnnotationValidationResults; | |
var success = _dataAnnotationsValidator.TryValidate(message, out dataAnnotationValidationResults); | |
AddDataAnnotationValidationResultsToValidationResultObject(dataAnnotationValidationResults); | |
return success; | |
} | |
private void AddDataAnnotationValidationResultsToValidationResultObject( | |
List<System.ComponentModel.DataAnnotations.ValidationResult> dataAnnotationValidationResults) | |
{ | |
foreach (var item in dataAnnotationValidationResults) | |
{ | |
_result.AddError(new ValidationFailure(item.MemberNames.FirstOrDefault(), item.ErrorMessage)); | |
} | |
} | |
public abstract ValidationResult Validate(IRequest message); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment