Ejemplo n.º 1
0
 static void CreatingAndUsingValidatorsDirectly()
 {
     // Create a Contains Characters Validator and use it to validate a String value.
     Validator charsValidator = new ContainsCharactersValidator("cat", ContainsCharacters.All,
                                                                  "Value must contain {4} of the characters '{3}'.");
     Console.WriteLine("Validating a string value using a Contains Characters Validator...");
     charsValidator.Tag = "Validating the String value 'disconnected'";
     // This overload of the Validate method returns a new ValidationResults
     // instance populated with any/all of the validation errors.
     ValidationResults valResults = charsValidator.Validate("disconnected");
     // Create a Domain Validator and use it to validate an Integer value.
     Validator integerValidator = new DomainValidator<int>("Value must be in the list 1, 3, 7, 11, 13.",
                                                              new int[] { 1, 3, 7, 11, 13 });
     integerValidator.Tag = "Validating the Integer value '42'";
     Console.WriteLine("Validating an integer value using a Domain Validator...");
     // This overload of the Validate method takes an existing ValidationResults
     // instance and adds any/all of the validation errors to it.
     integerValidator.Validate(42, valResults);
     // Create an Or Composite Validator containing two validators.
     // Note that the NotNullValidator is negated to allow NULL values.
     Validator[] valArray = new Validator[] {
           new NotNullValidator(true, "Value can be NULL."),
           new StringLengthValidator(5, RangeBoundaryType.Inclusive, 5, RangeBoundaryType.Inclusive,
                                         "Value must be between {3} ({4}) and {5} ({6}) chars.")
         };
     Validator orValidator = new OrCompositeValidator("Value can be NULL or a string of 5 characters.", valArray);
     // Validate two strings using the Or Composite Validator.
     Console.WriteLine("Validating a NULL value using an Or Composite Validator...");
     orValidator.Validate(null, valResults);  // this will not cause a validation error
     Console.WriteLine("Validating a string value using an Or Composite Validator...");
     orValidator.Validate("MoreThan5Chars", valResults);
     // Validate a single property of an existing class instance.
     // First create a Product instance with an invalid ID value.
     IProduct productWithID = new Product();
     PopulateInvalidProduct(productWithID);
     // Create a Property Value Validator that will use a RegexValidator
     // to validate the property value.
     Validator propValidator = new PropertyValueValidator<Product>("ID",
                         new RegexValidator("[A-Z]{2}[0-9]{4}", "Product ID must be 2 capital letters and 4 numbers."));
     Console.WriteLine("Validating one property of an object using a Property Value Validator...");
     propValidator.Validate(productWithID, valResults);
     // Now display the results of all the previous validation operations.
     ShowValidationResults(valResults);
 }
Ejemplo n.º 2
0
 static void ValidatingACollectionOfObjects()
 {
     // Create a collection of two objects, one with valid values and one with invalid values.
     List<Product> productList = new List<Product>();
     Product validProduct = new Product();
     PopulateValidProduct(validProduct);
     productList.Add(validProduct);
     Product invalidProduct = new Product();
     PopulateInvalidProduct(invalidProduct);
     productList.Add(invalidProduct);
     Console.WriteLine("Created and populated a collection of the Product class.");
     // Create an Object Collection Validator for the collection type.
     Validator collValidator = new ObjectCollectionValidator(typeof(Product));
     // Validate all of the objects in the collection.
     ValidationResults results = collValidator.Validate(productList);
     // Display the results.
     ShowValidationResults(results);
 }
Ejemplo n.º 3
0
 static void ValidatingParametersInAWCFService()
 {
     bool success;
     // Create a client to access the ProductService WCF service.
     ProductService.IProductService svc = new ProductService.ProductServiceClient();
     Console.WriteLine("Created a client to access the ProductService WCF service.");
     Console.WriteLine();
     // Call the method, which returns success or failure, to add a valid product.
     IProduct validProduct = new Product();
     PopulateValidProduct(validProduct);
     try
     {
         Console.WriteLine("Adding a valid product using the ProductService... ");
         success = svc.AddNewProduct(validProduct.ID, validProduct.Name, validProduct.Description,
                                       validProduct.ProductType, validProduct.InStock,
                                                                 validProduct.OnOrder, validProduct.DateDue);
         Console.WriteLine("Successful: {0}", success);
     }
     catch (FaultException<ValidationFault> ex)
     {
         // Validation of the Product instance failed within the service interface.
         Console.WriteLine("Validation within the ProductService failed.");
         // Convert the validation details in the exception to individual
         // ValidationResult instances and add them to the collection.
         ValidationResults results = ConvertToValidationResults(ex.Detail.Details, validProduct);
         // Display information about the validation errors
         ShowValidationResults(results);
     }
     catch (Exception ex)
     {
         Console.WriteLine("Error while calling service method: {0}", ex.Message);
     }
     Console.WriteLine();
     // Now call the same method to add an invalid product.
     IProduct invalidProduct = new Product();
     PopulateInvalidProduct(invalidProduct);
     try
     {
         Console.WriteLine("Adding an invalid product using the ProductService... ");
         success = svc.AddNewProduct(invalidProduct.ID, invalidProduct.Name, invalidProduct.Description,
                                       invalidProduct.ProductType, invalidProduct.InStock,
                                                                 invalidProduct.OnOrder, invalidProduct.DateDue);
         Console.WriteLine("Added an invalid product using the ProductService. Successful: {0}", success);
     }
     catch (FaultException<ValidationFault> ex)
     {
         // Validation of the Product instance failed within the service interface.
         Console.WriteLine("Validation within the ProductService failed.");
         // Convert the validation details in the exception to individual
         // ValidationResult instances and add them to the collection.
         ValidationResults results = ConvertToValidationResults(ex.Detail.Details, invalidProduct);
         // Display information about the validation errors
         ShowValidationResults(results);
     }
     catch (Exception ex)
     {
         Console.WriteLine("Error while calling service method: {0}", ex.Message);
     }
 }
Ejemplo n.º 4
0
 static void UsingAValidationRuleSetToValidateAnObject()
 {
     // Create a Product instance that contains invalid values.
     Product myProduct = new Product();
     PopulateInvalidProduct(myProduct);
     Console.WriteLine("Created and populated an invalid instance of the Product class.");
     Console.WriteLine();
     // Create a type validator for this type, specifying the rule set to use.
     Validator<Product> productValidator = valFactory.CreateValidator<Product>("MyRuleset");
     // Validate the Product instance to obtain a collection of validation errors.
     ValidationResults results = productValidator.Validate(myProduct);
     // Display the contents of the validation errors collection.
     ShowValidationResults(results);
 }