Пример #1
0
        static void UsingValidationAttributesAndSelfValidation()
        {
            // Create and populate a product instance with invalid values.
            IProduct invalidProduct = new AttributedProduct();

            PopulateInvalidProduct(invalidProduct);
            Console.WriteLine("Created and populated an invalid instance of the AttributedProduct class.");
            // Create a validator for this type. Must use the actual product type.
            // This will use the default rule set unless one is specified as the parameter.
            Validator <AttributedProduct> productValidator = ValidationFactory.CreateValidator <AttributedProduct>();

            // Validate the instance to obtain a collection of validation errors.
            ValidationResults results = productValidator.Validate(invalidProduct);

            // Alternatively, you could create and execute an ObjectValidator directly using:
            //   ValidationResults results = new ObjectValidator().Validate(validProduct);
            // Now display the contents of the validation errors collection.
            ShowValidationResults(results);
        }
Пример #2
0
 static void UsingValidationAttributesAndSelfValidation()
 {
     // Create and populate a product instance with invalid values.
     IProduct invalidProduct = new AttributedProduct();
     PopulateInvalidProduct(invalidProduct);
     Console.WriteLine("Created and populated a valid instance of the AttributedProduct class.");
     // Create a validator for this type. Must use the actual product type.
     // This will use the default rule set unless one is specified as the parameter.
     Validator<AttributedProduct> productValidator = valFactory.CreateValidator<AttributedProduct>();
     // Validate the instance to obtain a collection of validation errors.
     ValidationResults results = productValidator.Validate(invalidProduct);
     // Alternatively, you could create and execute an ObjectValidator directly using:
     //   ValidationResults results = new ObjectValidator().Validate(validProduct);
     // Now display the contents of the validation errors collection.
     ShowValidationResults(results);
 }