Attribute, when is Validation actually done?

Topics: Validation Application Block
May 22, 2007 at 3:24 PM
Hi there,

I started using the validation block today and tried something like that:
        [NotNullValidator(MessageTemplateResourceName = "UserNameValid", MessageTemplateResourceType = typeof(Resources))]
        [StringLengthValidator(1,50,MessageTemplateResourceName="UserNameValid", MessageTemplateResourceType=typeof(Resources))]
        private string m_name;

A Method is changing the value of m_name by setting it to Null but nothing happens. I don't use a config-file (that means none with an Validation-Configuration-Block.
I think one of those might be the problem:

- private is not available to the VAB
- ResourceType or -Name is wrong
- the validation doesn't run at all, but why?


When is a validation fired if given in Attributes?

I am really lost and would appreciate any sort of help.

Thanks a lot

Claudia
May 22, 2007 at 4:16 PM
I recommend checking out the documentation and viewing the quickstarts for options on how to use the Validation Application Block.

Your example might look like:

public class Customer
{
 
    private string m_name;
 
    [NotNullValidator(MessageTemplateResourceName = "UserNameValid", MessageTemplateResourceType = typeof(Resources))]
    [StringLengthValidator(1,50,MessageTemplateResourceName="UserNameValid", MessageTemplateResourceType=typeof(Resources))]
    public string Name
    {
        get { return m_name; }
        set { m_name  = value; }
    }
}

and then call for Validation on an instance of the class:

Customer customer = new Customer();
 
ValidationResults results = Validation.Validate(customer);
if (!results.IsValid)
{
    // ...
}

Regards,

Dave

________________________________

David Hayden
Microsoft MVP C#
May 23, 2007 at 7:06 AM
Thank you for that hint.

I did read documentation and QuickStart. There was something about Attributetargets and there were Properties and Fields mentioned, so I thought I could do it the short way. For my understanding: I have to use public members or is it because of using a field instead of a property?

So I always have to start the validation explicitely, the attributes are only giving the configuration?

Thanks a lot

Claudia

May 23, 2007 at 1:21 PM
You can define an attribute on a field:

[NotNullValidator(MessageTemplateResourceName = "UserNameValid", MessageTemplateResourceType = typeof(Resources))]
[StringLengthValidator(1,50,MessageTemplateResourceName="UserNameValid", MessageTemplateResourceType=typeof(Resources))]
public string Name;

Exactly, the attributes are only describing the validation rules.

You need to call Validation explicitly unless you use the Policy Injection Application Block to call validation for you when you pass an instance of the class as a parameter. For example, if you create an interceptable CustomerDAO Class to save a customer to a database:

public class CustomerDAO : MarshalByRefObject
{
	[ValidationCallHandler]
	public void Save(Customer customer)
	{
		// Do Something
	}
}

The Policy Injection Application Block will intercept the Save Method on CustomerDAO an do validation on the customer object before running the method. If it is invalid, it will throw an ArgumentValidationException that you can catch and properly handle. This is a little more advance use of Validation and comes at a bit of a performance cost, but you don't have to always explicity call the validation yourself.

CustomerDAO dao = PolicyInjection.Create<CustomerDAO>();
 
try
{
	dao.Save(customer);
}
catch (ArgumentValidationException ex)
{
	// customer is not valid...
}

You can read more about the Policy Injection Application Block in the documentation as well as check out the quickstarts.

Regards,

Dave

______________________________

David Hayden
Microsoft MVP C#