Defining a Declarative Rule Condition

To illustrate defining a declarative rule condition, you will modify the previous While activity. First, you need to set
the
Condition property of the While activity to Declarative Rule Condition, rather than Code Condition. At
this point, the existing
GetAndValidateUserName() method could be deleted, as it will no longer be used. You also
need to provide a new way to obtain the user name, as this was done in the
GetAndValidateUserName() method.


















Next, you need to give your declarative rule a name and conditional statement. This can be done using the integrated
rule editor of Visual Studio. Simply click on the ellipse button (…) option of the
ConditionName property.




















Once you click the New button, you can then define the rule. Notice the following:
      •        You have access to the members of the WF class.
      •        You are specifying the same condition you entered in the previous
GetAndValidateUserName()
                
method.


Me.UserName.Length >= Me.NameLength















You can enter either C# or VB code in the rule editor. However, once you click the OK button, VB tokens such as
the
Me keyword are automatically translated into C# equivalents (e.g., the this keyword).

After you click the OK button, you have a chance to name your condition. Assume you have named your condition

IsNameLengthValid
. Here is the end result of your operations.



















When you define declarative rules, the IDE will create a brand-new *.rules file that is connected to the current
workflow instance. You can view this new file via the Solution Explorer.





















If you were to open this *.rules file, you would find markup similar to the following. Notice that there are ‘left’ and
‘right’ sides of the expression. Also notice the
CodeBinaryOperatorExpression element defines an attribute
named
Operator, which represents the binary operator used in the rule.


<RuleDefinitions xmlns="http://schemas.microsoft.com/winfx/2006/xaml/workflow">
<RuleDefinitions.Conditions>
<RuleExpressionCondition
Name="IsNameLengthValid">
<RuleExpressionCondition.Expression>
  <ns0:CodeBinaryOperatorExpression
   
 Operator="GreaterThanOrEqual"
    xmlns:ns0="clr-namespace:System.CodeDom;Assembly=System, Version=2.0.0.0,
               Culture=neutral, PublicKeyToken=b77a5c561934e089">

    <ns0:CodeBinaryOperatorExpression.Left>
      <ns0:CodePropertyReferenceExpression PropertyName="Length">
         <ns0:CodePropertyReferenceExpression.TargetObject>
           <ns0:CodePropertyReferenceExpression
PropertyName="UserName">
             <ns0:CodePropertyReferenceExpression.TargetObject>
               <ns0:CodeThisReferenceExpression />
             </ns0:CodePropertyReferenceExpression.TargetObject>
           </ns0:CodePropertyReferenceExpression>
         </ns0:CodePropertyReferenceExpression.TargetObject>
      </ns0:CodePropertyReferenceExpression>
    </ns0:CodeBinaryOperatorExpression.Left>

    <ns0:CodeBinaryOperatorExpression.Right>
      <ns0:CodePropertyReferenceExpression PropertyName="NameLength">
        <ns0:CodePropertyReferenceExpression.TargetObject>
          <ns0:CodeThisReferenceExpression />
        </ns0:CodePropertyReferenceExpression.TargetObject>
      </ns0:CodePropertyReferenceExpression>
    </ns0:CodeBinaryOperatorExpression.Right>

  </ns0:CodeBinaryOperatorExpression>
</RuleExpressionCondition.Expression>
</RuleExpressionCondition>
</RuleDefinitions.Conditions>
</RuleDefinitions>


The data within a *.rules file can either be shipped as a standalone file or embedded within the assembly as a binary
resource. The default behavior is to embed the markup as a binary resource. If necessary, stand alone *.rules files
can be dynamically read in to memory at runtime.
Defining a Declarative Rule Condition
Table of Contents
Copyright (c) 2008.  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.
Courseware
Training Resources
Tutorials
Services