ETL - Validation

Topics: Validation Application Block
Dec 2, 2011 at 11:31 PM

Hi,

I created two classes. A base class "A" and a "B" class that inherits from "A". I need to call the validator class "B" and it validates all of the properties but did not validate in class "A".
Which code to use to validate class "B" and automatically class A?

public classA{

[required]

public string direccion { get; set; }

}

public classB{

[required]

public string direccion { get; set; }

}

Dec 3, 2011 at 4:13 AM

Assuming you have two classes, ClassA and ClassB:

    public class ClassA
    {
        [Required]
        public string Direction { get; set; }

    }
    
    public class ClassB : ClassA
    {
        [Required]
        public string Time { get; set; }
    }

You can validate an instance of ClassB which will also validate the base class (ClassA) using the following code:

    var classB = new ClassB();

    Validator<ClassB> validator = ValidationFactory.CreateValidator<ClassB>();
    ValidationResults validationResults = validator.Validate(classB);

    foreach (Microsoft.Practices.EnterpriseLibrary.Validation.ValidationResult result in validationResults)
    {
        Console.WriteLine(result.Message);
    }

Which will produce the following output:

The Time field is required.
The Direction field is required.

 

--
Randy Levy
Enterprise Library support engineer
entlib.support@live.com