Xml and property validation

Topics: User Forum
Sep 11, 2007 at 3:39 AM
I have a custom class. This class has many properties. I want to use XML file to use all the rules. How do I force the validation to happen?
Example:
Class Person
' private members
' Public properties

' Static method
' -- want to validate all the properties here and not before
End Class
Coordinator
Sep 11, 2007 at 11:25 AM
Edited Sep 11, 2007 at 11:26 AM
If you want to do your own validating you can use the following code. (Modified from the ConfigurationService sample in the chm)

using NUnit.Framework;
using ValidationFramework;
using ValidationFramework.Configuration;
 
namespace ExampleLibraryCSharp
{
 
    [TestFixture]
    public class ConfigurationServiceSample
    {
        private const string personValidationXml = @"<?xml version='1.0' encoding='utf-8' ?>
<validationMapping xmlns='urn:validationFramework-validationDefinition-1.5'>
  <class 
    typeName='ExampleLibraryCSharp.ConfigurationServiceSample+Person, ExampleLibraryCSharp'>
    <property 
      name='Name'>
      <rule 
        typeName='RequiredStringRuleConfigReader'/>
    </property>
    <property 
      name='Age'>
      <rule 
        typeName='RangeRuleConfigReader' 
        minimum='18' 
        maximum='80' 
        errorMessage='Age is invalid' />
    </property>
  </class>
</validationMapping>";
 
        public class Person
        {
        
            private string name;
            private int age;
 
            public string Name
            {
                get
                {
                    return name;
                }
                set
                {
                    name = value;
                }
            }
 
            public int Age
            {
                get
                {
                    return age;
                }
                set
                {
                    age = value;
                }
            }
 
           
            public static bool Validate(Person person)
            {
             PropertyValidationManager propertyValidationManager = new PropertyValidationManager(person);
                propertyValidationManager.ValidateAllProperties();
                return propertyValidationManager.IsValid;
            }
         
        }
 
        [Test]
        public void MyProgram()
        {
            //This line should only be run once on application startup
            ConfigurationService.AddXmlString(personValidationXml);
 
 
            Person person = new Person();
            
            bool valid;
 
            valid = Person.Validate(person);
            Assert.IsFalse(valid);
 
            person.Age = 25;
            person.Name = "Smith";
 
            valid = Person.Validate(person);
            Assert.IsTrue(valid);
        }
    }
}

Alternatively, an i think easier, you could simply inherit from ValidatableBase. The properties are only validated when IsValid is called.

using NUnit.Framework;
using ValidationFramework;
using ValidationFramework.Configuration;
 
namespace ExampleLibraryCSharp
{
 
    [TestFixture]
    public class ConfigurationServiceSample
    {
        private const string personValidationXml = @"<?xml version='1.0' encoding='utf-8' ?>
<validationMapping xmlns='urn:validationFramework-validationDefinition-1.5'>
  <class 
    typeName='ExampleLibraryCSharp.ConfigurationServiceSample+Person, ExampleLibraryCSharp'>
    <property 
      name='Name'>
      <rule 
        typeName='RequiredStringRuleConfigReader'/>
    </property>
    <property 
      name='Age'>
      <rule 
        typeName='RangeRuleConfigReader' 
        minimum='18' 
        maximum='80' 
        errorMessage='Age is invalid' />
    </property>
  </class>
</validationMapping>";
 
        public class Person : ValidatableBase
        {
        
            private string name;
            private int age;
 
            public string Name
            {
                get
                {
                    return name;
                }
                set
                {
                    name = value;
                }
            }
 
            public int Age
            {
                get
                {
                    return age;
                }
                set
                {
                    age = value;
                }
            }
         
        }
 
        [Test]
        public void MyProgram()
        {
            //This line should only be run once on application startup
            ConfigurationService.AddXmlString(personValidationXml);
 
            Person person = new Person();
 
            Assert.IsFalse(person.IsValid);
 
            person.Age = 25;
            person.Name = "Smith";
 
            Assert.IsTrue(person.IsValid);
        }
    }
}

Hope that helps
Sep 11, 2007 at 9:44 PM
Thank you very much Simon. That helps a lot.
Coordinator
Sep 12, 2007 at 12:47 AM
Cool

Also note that the syntax '+' is used to identify the Person class only because it is an inner class of ConfigurationServiceSample. i.e. 'ExampleLibraryCSharp.ConfigurationServiceSample+Person, ExampleLibraryCSharp'

Your type name would probably be something like this
'MyComanpany.MyProject.Person, MyProject'