Adding specific validation messages to value objects

One of the requests we often get in projects that use the Adf.Net framework is to be able to get specific validation messages, especially when during persisting panels to domain objects, properties that have value objects as their type fail to persist, usually during call such as below.

protected void lbOpslaan_Click(object sender, EventArgs e)
    BindManager.Persist(Persoonsgegevens, panelPersoonsgegevens);


I have added a mechanism to Adf.Net that supports three levels of validation messages. When the ValueObjectPropertyParser is unable to create a valid instance of a value object, it will act as follows.

First it will look if a specific validation message is present at the property itself it tries to create. Use the following attribute ValidationMessageAttribute to define such as message.

public PhoneNumber Telefoonnummer
    get { return Get<PhoneNumber>(PersoonsgegevensDescriber.Telefoonnummer); }
    set { Set(PersoonsgegevensDescriber.Telefoonnummer, value); }

The message defined will show during validation only if this specific property for this domain object fails to instantiate.

If such an attribute is not present, the parser will look at the type of the property. In this specific example it is PhoneNumber. At the class (or struct) you can now define a ValidationTypeMessageAttribute attribute as follows.

public struct PhoneNumber : IValueObject, IComparable {

This message will show for any property of this type in any domain object that fails to be instantiated. Except of course for those properties that have the ValidationMessageAttribute attribute defined.

If no such attribute is defined on the class or struct, the ValueObjectPropertyParser will display the default validation message, which is defined by the code Adf.Business.NotInstantiable.

Please note that, in any case, normal handling of validation takes place. The defined attributes should use always a code, where the actual message is stored in resource files. By default all three messages take two arguments that can be used as parameters in the message. The first argument {0} is set to the improper value, and the second argument {1} is set to the property that could not be instantiated, with the following result.