Details of Rule Construction
A WF rule always breaks down to three parts, one of which is technically optional:
- A single condition that evaluates to true or false.
- A ‘then’ action that will execute if the condition is true.
- An optional ‘else’ action that will execute if the condition is false.
Essentially, a rule allows you to represent a simple C# / VB if…else…then statement in a declarative manner. Like an if…else…then code statement, the rule condition can be created using a set of operators. Here are the valid rule operators that can be used when creating a WF rule condition.
|>, >=, <, <=||Greater / less than operators|
&&, ||, ! (C#)
AND, OR, NOT (VB)
|&, |||Bitwise operators|
|+, -, *, /||Binary mathematical operators|
|( )||Parentheses can be use to define operator precedence|
|[ ]||Indexer for arrays or collection objects (e.g., myInts, myListOfPeople["Homer"])|
At runtime, the markup used to define a rule condition is represented in memory as an instance of the System. Workflow.Activities.Rules.Rule class type. The Condition property that encapsulates an object derives from RuleCondition, which represents the condition itself. The ElseActions and ThenActions properties represent the two possible outcomes based on the rule.
These properties encapsulate a collection of RuleAction objects. RuleAction is the abstract parent for derived types and represent details regarding the ‘else’ or ‘then’ action to take.
In addition, the Rule type defines a Priority property, which encapsulates an integer. By default, a Rule object has a Priority level of zero. When you construct ‘rule sets’ (examined later in this chapter), the Priority value can be used to influence when a rule within a set executes. If multiple rules in a rule set are assigned the same priority, they will be evaluated alphabetically.
Be aware that the need to work directly in code with the Rule type and the numerous related types is rare. In most cases, you can remain blissfully unaware of how the XML rule descriptions map to the runtime object model.
In the rare case that you need to work with ‘declarative rules’ programmatically, you can do so. Be aware, however, that internally the rules object model makes extensive use of the System.CodeDom API. This class will not examine how to define a declarative rule programmatically. Consult the .NET Framework SDK documentation for details.
Copyright (c) 2008-2013. Intertech, Inc. All Rights Reserved. This information is to be used exclusively as an online learning aid. Any attempts to copy, reproduce, or use for training is strictly prohibited.