How to Validate a BusinessObject

Topics: Validation Application Block
Oct 20, 2011 at 8:59 PM

I'm starting to look into the Validation Block and it seems pretty easy to validate each property of a class such a Customer individually, see below.

public class Customer
        [StringLengthValidator(1, 25)]
        public string FirstName { get; set; }
        [StringLengthValidator(1, 25)]
        public string LastName { get; set; }
        public string SSN { get; set; }
        public Address Address { get; set; }

But what if I want to validate the Customer class based on following criteria for example.
1. First name must be more the 5 chars only if Last Name is 15 characters.
2. SSN can be 10 chars or more if First Name is not null
Basically instead of validating one property I check a combination of them.

I haven't found an example that does a good job explaining what I want to do,  any help would be very , very appreciated.


Oct 20, 2011 at 10:47 PM

You need Self Validation. Here is an example: Using Self Validation.

Oct 21, 2011 at 2:38 AM

Thanks a lot, you have no idea the headache this caused me. I was going in circles trying to use a custom validator and it just wasn't working for me.  Is there a way to implement a custom validator for what I want to do?

Oct 21, 2011 at 11:17 AM

There are basically two ways of implementing custom validations. One is self validation, the other is writing custom validators. Self validation is much easier, but a custom validator does do a better job in separating concerns.