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.