Updating Rules Dynamically


One of the things that make rules so flexible is that they can be updated, even during runtime. Imagine a scenario where a workflow instance is running and reaches a point where it cannot continue because a rule has evaluated a certain way. This could happen when new customer gets rejected because of a credit check’s results. However, a credit specialist might have the authority to lower the credit-score threshold. Because of situations like this, workflow rules can be changed fairly easily.

Generally, changes occur based on some code being called in the workflow host. A reference to the workflow instance in question must be acquired, because dynamic changes to rules occur at an instance level, not at the definition level. After a workflow instance reference is obtained, there are a few steps you must take to obtain a reference to a RulesDefinition object. After you have this object, you can make changes to rules programmatically, as shown earlier.

The following code is called from the workflow host when a request is made from the workflow to approve a decrease in the minimum credit score. This would likely happen in a CallExternalMethod activity. The workflow then determines which actions were taken by the user, if any, and proceeds appropriately.

  static void ModifyMinimumCreditScore(WorkflowInstance instance) {     // this value is hardcoded for the example     int creditScoreFloor = 600;     WorkflowChanges workflowChanges =         new WorkflowChanges(instance.GetWorkflowDefinition());     CompositeActivity rootActivity = workflowChanges.TransientWorkflow;     RuleDefinitions ruleDefinitions = (RuleDefinitions)rootActivity.GetValue(         RuleDefinitions.RuleDefinitionsProperty);     // loop through the rules collection and find the right one     IEnumerator<Rule> rulesEnumerator =         ruleDefinitions.RuleSets[0].Rules.GetEnumerator();     Rule currentRule = null;     while (rulesEnumerator.MoveNext())     {         currentRule = rulesEnumerator.Current;         if (currentRule.Name == "CheckMinCreditScore")         {             break;         }     }     // make sure we found our rule     if (currentRule != null)     {         RuleExpressionCondition ruleCondition =             (RuleExpressionCondition)currentRule.Condition;         CodeBinaryOperatorExpression condition =             (CodeBinaryOperatorExpression)ruleCondition.Expression;         // update the condition with the new credit score floor         condition.Right = new CodePrimitiveExpression(creditScoreFloor);         // apply the changes         instance.ApplyWorkflowChanges(workflowChanges);     } } 

This code example uses the concept of dynamic update to change a minimum credit score from whatever it was to 600. WorkflowChanges and its TransientWorkflow property facilitate this process by allowing access to the activities and rules inside a running workflow instance. After using some of the CodeDom classes to create the new rule expression, the ApplyWorkflowChanges method of the WorkflowInstance class is what finally cements the change in the workflow. The concepts illustrated in this code example are new and are part of the broader dynamic update topic that is discussed in detail in Chapter 11.



Professional Windows Workflow Foundation
Professional Windows Workflow Foundation
ISBN: 0470053860
EAN: 2147483647
Year: 2004
Pages: 118
Authors: Todd Kitta

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net