In this second chapter, we explain all validations forms in DataAnnotations. We will review Validator class with all its structure.Let's go !!!.
DataAnnotations enables us to work with positive programming, in other words, allows us anticipate exceptions for problems in our business classes or domain classes. It provides better performance, also exists the negative programming version.
- Positive àTryValidate.
- Negative à Validate.
In this Link, there is an information about Exceptions and Performance.
The examples class
The class for all examples, will be a new version of Chapter I class.
These are the principal validations classes:
ValidationResult is a container class validation results. More info Link.
ValidationResult has 2 properties:
- ErrorMessages .- String readonly property with the information error description.
- MemberNames.- IEnumerable<string> readonly property with the property name on error.
We have written an extension method to print console the validation errors:
This is the static helper class. The Validator class allows you execute validation on objects, properties and methods. More information Link.
We can separate its methods in two groups, as we saw earlier:
POSITIVE PROGRAMMING (Return bool value and we have a ICollection<ValidationResult> parameter)
The return property bool is a validation result. The ICollection<ValidationResult> argument contains details of validations errors. If the validation isn’t errors, this collection will be empty.
This method validates all object. TryValidateObject has an overload with a bool argument validateAllProperties, this argument enables in case of validation error continue with the validation all properties.
- Instance.- The instance of class to validate.
- ValidationContext.- The context that describes the object to validate.
- ValidationResults .- The collection of validation results descriptions.
- ValidateAllProperties.- Enabled continue validate all properties.
In this example, we have done the second overload with true value (ValidateAllProperties).
It has only one overload. It has the same parameters of TryValidateObject, but the first object parameter, does match with one property to validate and the method therefore validates properties.
When we instance the ValidationContext object, we give value to string property MemberName, with the property name to validate.
The TryValidateProperty and the TryValidateObject, are equals, only change the first parameter. In the TryValidateProperty will have to be said the property value.
TryValidateValue validates a value through ValidationAttribute collection. This is practical to reuse our ValidationAttributes and us to be disabuses of if terms.
We have created the attributes in code, because we will validate a value and we don’t have properties to mark in the class declaration.
NEGATIVE PROGRAMMING (throw ValidationException and it hasn’t ICollection<ValidationResult argument )
These methods, remove the word Try in your names. They are void methods. If the validation fail, will be throw a ValidationException. They don’t have a ICollection<ValidationResult> argument, this information will be have inside exception and it isn’t a collection, it is a simple ValidationResult because if validation fail doesn’t verify remaining properties.
The others methods are the same than ‘Try’ version but with this technique.
Considerations and tests
Remember, we can use the extension method of Chapter I.