Using XML configuration

Jun 11, 2007 at 2:33 PM
Hi Simon,

I'm not exactly understood how to use the Validation Framework with an XML configuration files.
I have class library project with a business entity class (supposing Person) and an XML file Person.validation.xml that defines a desired validation rules as embedded resource.

How can I join the Person class and the XML configuration file so that my test WinForms application will use this business entity properly?

Thanks in advance!

P.S.sorry about my english :-(
Jun 12, 2007 at 2:04 AM
Your English is fine.

Have a look at the CHM under “Adding Xml Mapping Document”. At the bottom it states

Adding any xml segment is done through the ConfigurationService. For a full list of the input types supported see the documentation of ConfigurationService.

The recommended way of adding an xml segment is by including it as an embedded resource. Then ConfigurationService.AddAssembly is called on the assembly in question. The ConfigurationService will then add the contents each resource that end with ".validation.xml”.

It is also recommended that a ".validation.xml” file exists for each type that requires validation. So the type “Person” would have a matching embedded resource file called "Person.validation.xml".

When your application starts you should call ConfigurationService.AddAssembly and pass the Assembly that “Person” exists in.

For example.
ConfigurationService.AddAssembly(Assembly.GetExecutingAssembly());
This example assumes that the "Person" embedded resources exists in the current executing assembly.

Also have a look at this http://www.codeplex.com/ValidationFramework/Thread/View.aspx?ThreadId=9127

Hope that helps
Jun 19, 2007 at 8:50 AM
Edited Jun 19, 2007 at 8:55 AM
Hi Simon,

If I understood right, I need to call the ConfigurationService.AddAssembly method in my Person class constructor and in each property set I need write something as:

public string FirstName
{
get { return this._firstName; }
set
{
string strOldValue = this._firstName;
this._firstName= value;
base.ValidateProperty("FirstName");
if (!base.IsValid)
{
this._firstName= strOldValue;
throw new ArgumentException(base.Error);
}
}
}

My Person class inherits from the ValidationFramework.NotifyValidatableBase class.

Right?

Thanx in advance!
Jun 27, 2007 at 10:31 AM
This discussion has been copied to a work item. Click here to go to the work item and continue the discussion.
Jun 27, 2007 at 10:41 AM
Levik
Sorry about the delayed response.
You only need to call ConfigurationService.AddAssembly once (for each assembly) in the lifetime of your application. The validation rules are then cached. So you would call ConfigurationService.AddAssembly either in the Application_Start global.asax.cs of a web site or the Main method for an application. It only has to occur before you perform your validations.

On the latest checkin i also added some code that will make it easier to validate the value on the property set without using temp variables
Note that with this code you are making a distinction between invalid data and exceptional data.

[RequiredStringRule]
[LengthStringRule(50)]
public string FirstName
{
  get
  {
    return firstName;
  }
  set
  {
    PropertyValidationManager.ThrowPropertyException(this, value, "FirstName", null, null);
    firstName = value;
  }
}