Simple WF Activity Validator

Link. February 17, 2006. Comments [0]. Posted in: WinFX | Workflow

If you're building custom Windows Workflow Foundation activities, then you'll likely want to write your own custom ActivityValidator-derived classes, in order to provide your own custom validation logic to ensure activities are correctly configured at design time.

Here's a very simple sample ActivityValidator class:

   /// <summary>

   /// Validator class for the MsmqReceiveActivity

   /// </summary>

   public class MsmqReceiveActivityValidator : ActivityValidator

   {

      /// <summary>

      /// Validate the receive activity

      /// </summary>

      /// <param name="manager">Validation Manager to use</param>

      /// <param name="obj">Activity instance to validate</param>

      /// <returns>Collection of validation errors</returns>

      public override ValidationErrorCollection Validate(ValidationManager manager, object obj)

      {

         ValidationErrorCollection errors = base.Validate(manager, obj);

         MsmqReceiveActivity act = (MsmqReceiveActivity)obj;

 

         if ( !act.IsBindingSet(MsmqReceiveActivity.QueueProperty) )

         {

            if ( act.GetValue(MsmqReceiveActivity.QueueProperty) == null )

            {

               errors.Add(ValidationError.GetNotSetValidationError("Queue"));

            }

         }

 

         if ( act.MessageType == null )

         {

            errors.Add(ValidationError.GetNotSetValidationError("MessageType"));

         }

 

         if ( !act.IsBindingSet(MsmqReceiveActivity.MessageReceivedProperty) )

         {

            errors.Add(ValidationError.GetNotSetValidationError("MessageReceived"));

         }

         return errors;

      }

 

   } // class MsmqReceiveActivityValidator

This validator simply does the following checks:

  1. Ensures that the Queue property (which is a dependency property) of the activity is either bound to an external property, or has a direct value.
  2. Ensures that the MessageType property of the activity has an actual value assigned to it.
  3. Ensures that the MessageReceive property of the activity (an attached dependency property) has been explicitly bound.

The validator is then attached to the activity via the [ActivityValidator] attribute, like this:

[SRDescription("Recv_Description")]

[ToolboxItem(typeof(ActivityToolboxItem))]

[Designer(typeof(MsmqReceiveActivityDesigner), typeof(IDesigner))]

[ToolboxBitmap(typeof(MsmqReceiveActivity), "Resources.MessageQueuing.bmp")]

[ActivityValidator(typeof(MsmqReceiveActivityValidator))]

public partial class MsmqReceiveActivity

   : Activity, IEventActivity, IActivityEventListener<QueueEventArgs>

 



Comments are closed.

Syndicate

About

Tomas Restrepo is a software developer located in Colombia, South America. His interests include .NET, Connected Systems, PowerShell and lately dynamic programming languages. More...

tomasrestrepo @ twitter My Flickr photostream My saved links on delicious My Technorati Profile

email: tomas@winterdom.com
msn: tomasr@passport.com

View my profile on LinkedIn

MVP logo

Ads


Categories

Statistics

Total Posts: 1041
This Year: 111
This Month: 1
This Week: 0
Comments: 819

Archive

Other

Copyright © 2002-2008, Tomas Restrepo.

Powered by: newtelligence dasBlog 2.2.8279.16125

Sign In