Consider that we need to validate an instance of the Person class given below.
public class Contact
    {
        public string FirstName { get; set; } // Mandatory
        public string MiddleName { get; set; }
        public string LastName { get; set; } // Mandatory

        public string PersonalEmail { get; set; } // At least one of Personal or Business email is mandatory
        public string BusinessEmail { get; set; } 
        
        public Company Organization { get; set; }

        public Address BusinessAddress { get; set; } // Business address is mandatory if the person is currently working.
        public Address OtherAddress { get; set; }

        public Job CurrentJob { get; set; } // Current job is mandatory if the person is currently working.
    }

    public class Address
    {
        public string AddressLine1 { get; set; } // Mandatory
        public string AddressLine2 { get; set; } // Mandatory
        public string AddressLine3 { get; set; }
        public string AddressLine4 { get; set; }
        public string City { get; set; } // Mandatory
        public string StateOrCounty { get; set; }
        public string Country { get; set; } // Mandatory
        public string Zipcode { get; set; } // Mandatory
    }

    public class Company
    {
        public string Name { get; set; } // Mandatory
        public Address OfficeAddress { get; set; } // Mandatory
    }

    public class Job
    {
        public string Title { get; set; } // Mandatory
        public DateTime From { get; set; } // Mandatory
        public DateTime? To { get; set; } // Optional, should be greater than From
    }
  • Validating by writing a named validation.
           var validation = new Validation<Contact>("Contact_Validation_Example", new ValidationOptions { StopOnFirstError = false })
                .Setup(validator => validator
                                        .IsNotNullOrEmpty(c => c.FirstName)
                                        .IsNotNullOrEmpty(c => c.LastName)
                                        .IfThen(c => c.Organization != null, "Organization Title and Address are mandatory.", c => c.Organization.ValidateUsing("Company_Validation_Example"))
                                        .IfThen(c => c.Organization != null, "Current job details are missing.", c => c.CurrentJob.ValidateUsing("Job_Validation_Example"))
                                        .IfThen(c => c.Organization != null, "Business address is missing.",
                                                c => c.BusinessAddress.ValidateUsing("Address_Validation_Example"))
                );
            var organizationValidation = new Validation<Company>("Company_Validation_Example", new ValidationOptions { StopOnFirstError = false })
                .Setup(validator => validator
                                        .IsNotNullOrEmpty(c => c.Name)
                                        .PassesSavedValidation(c => c.OfficeAddress, "Address_Validation_Example")
                );
            var addressValidation = new Validation<Contract.Address>("Address_Validation_Example")
                .Setup(validator => validator
                                        .IsNotNullOrEmpty(c => c.AddressLine1)
                                        .IsNotNullOrEmpty(c => c.AddressLine2)
                                        .IsNotNullOrEmpty(c => c.City)
                                        .IsNotNullOrEmpty(c => c.StateOrCounty)
                                        .IsNotNullOrEmpty(c => c.Country)
                                        .IsNotNullOrEmpty(c => c.Zipcode)
                );
            var jobValidation = new Validation<Job>("Job_Validation_Example")
                .Setup(validator => validator
                                        .IsNotNullOrEmpty(j => j.Title)
                );

            var validationRepository = new ValidationRepositoryFactory().GetValidationRepository();
            validationRepository.Save(validation);
            validationRepository.Save(organizationValidation);
            validationRepository.Save(addressValidation);
            validationRepository.Save(jobValidation);
  • Once a named validation has been saved, it can be reused i code like a normal validator.
// Validate contact
var contactValidator = invalidContact.ValidateUsing("Contact_Validation_Example");
Assert.IsFalse(contactValidator.IsValid);

// Address job
var addressValidator = invalidAddress.ValidateUsing("Address_Validation_Example");
Assert.IsFalse(addressValidator.IsValid);
...

Last edited Jan 9, 2011 at 9:10 AM by ashishsharmaait, version 1

Comments

No comments yet.