Workflow Foundation 4.0 - Validation

Ambily.raj
Posted by in WWF category on for Intermediate level | Points: 250 | Views : 13607 red flag

Workflow Foundation 4.0 has very good support for custom activity development. One of the areas in custom activity development is validating the properties and its values in design time. In this article we will discuss about various validation options available with workflow foundation 4.0.

 Argument Validation

As we discussed earlier, the data model in WF4 is based on InArguments, OutArguments and InOutArguments. Here, we will discuss about the various ways to do the argument validation.

RequiredArgument

If the InArgument is a required argument, decorate the same with RequiredArgument attribute.
 
Custom Activity

public class CustomActivity:NativeActivity

    {

        [RequiredArgument()]

        public InArgument<string> InputData

        {

            get;

            set;

        }

       

        protected override void Execute(NativeActivityContext context)

        {         

        }

    }


Observe the design time validation error on the activity as well as in error window. 



 

Note: Validation errors are not compilation errors. Project with validation errors will compile successfully.

OverloadGroup

When you want to group the arguments use the OverloadGroup attribute. This will group the attributes and only one group of attribute should be valid. If you specify value for attributes belonging to two groups, it will throw validation error.

Custom Activity

public class CustomActivity:NativeActivity

    {

       

        [OverloadGroup("GroupedData")]

        public InArgument<string> InputData

        {

            get;

            set;

        }

 

        [OverloadGroup("GroupedData")]

        public InArgument<string> TextData

        {

            get;

            set;

        }

 

        [OverloadGroup("Data")]

        public InArgument<string> Data

        {

            get;

            set;

        }

       

        protected override void Execute(NativeActivityContext context)

        {         

        }

    }


Now, we specify the values for InputData and Data arguments, which belong to two groups. Observe the validation error.



Property Validation

Even though the WF4 data model is based on arguments, sometimes we may need to use properties. Property validations are mainly done using the CacheMetadata method. Override the cacheMetadata method and perform the validation


Custom Activity

public class CustomActivity:NativeActivity

    {       

        public string Data

        {

            get;

            set;

        }

 

        protected override void CacheMetadata(NativeActivityMetadata metadata)

        {

            base.CacheMetadata(metadata);

 

            ////Validation

            if (string.IsNullOrEmpty(this.Data))

            {

                metadata.AddValidationError("Data should not be blank.");

            }

        }

 

        protected override void Execute(NativeActivityContext context)

        {         

        }

    }


We can add any kind of validations inside CacheMetadata. In our example, verified whether the property value is not null or empty. If it is empty, throw the validation error.


Conclusion

WF4.0 has various methods for performing the design time validation for arguments and properties.

Page copy protected against web site content infringement by Copyscape

About the Author

Ambily.raj
Full Name: Ambily KK
Member Level: Silver
Member Status: Member,Microsoft_MVP,MVP
Member Since: 5/18/2010 1:05:25 AM
Country: India
Thanks Ambily K K http://ambilykk.com/
http://ambilykk.com/
I have over 9 years of experience working on Microsoft Technologies. I am carrying the passion on Microsoft technologies specifically on web technologies such as ASP .Net and Ajax. My interests also include Office Open XML, Azure, Visual Studio 2010. Technology adoption and learning is my key strength and technology sharing is my passion.

Login to vote for this post.

Comments or Responses

Posted by: Vuyiswamb on: 4/15/2011 | Points: 25
Thanks got Sharing this article , what is work flow ?

Posted by: Ambily.raj on: 4/16/2011 | Points: 25
Thanks

Please refer the Windows Workflow Foundation 4.0 tutorials from
_http://www.dotnetfunda.com/misc/page28.WWF-Tutorials.aspx

Regards
Ambily

Login to post response

Comment using Facebook(Author doesn't get notification)