views:

239

answers:

1

I'm unclear about when and how I should be doing validation in my custom activities/custom activity factories/custom designers in WF 4.0.

The only place within my activity that seems to provide validation error support is within the CacheMetadata method (I believe this is called multiple times during the design process). Is this where I should be doing my validation? Is there support for validation within the ActivityDesigner?

+2  A: 

Yes, CacheMetadata in your activity is where you should be doing your validation. Validation is a concept which applies to the workflow runtime as well as the designer. (You can see this from the fact that you can still attempt to run workflows, compiled or declarative, which have validation errors, but an exception will be thrown when you do.)

Example:

protected override void CacheMetadata(ActivityMetadata metadata)
{
   if (this.Body == 0) metadata.[AddValidationError](
     new ValidationError(
       "You forgot to supply a body for (this activity)",
       /*iswarning = */ true, 
       "Body"));
}

(See ActivityMetadata.AddValidationError[1] )

Your secondary question is is there support for adding validation from the ActivityDesigner side? The answer is 'no, not really' - it would be possible for to do some custom WPF and data binding which adds some 'custom validation' on top of the ActivityDesigner, maybe even use AttachedPropertiesService to define your own SatisfiesConstraints property on your activity class. But it's extra work, and it's not going to tie in with the runtime validation consistently, so it's a limited value idea.

Tim Lovell-Smith