Поделиться через


Advanced Policy Sample

Download sample

This sample extends the Simple Policy sample. In addition to the residential discount and business discount rules from the Simple Policy example, several new rules have been added.

A high-value rule is added, which provides a bigger discount for high-value orders. It is given a priority value less than the previous two rules so that it will overwrite the discount field and take precedence over either the residential or business discount rules.

A calculate total rule is also added, which computes the total based on the discount level. It shows how to reference a method defined on the workflow activity, as well as how to use else actions. This rule also demonstrates chaining behavior since it will be evaluated anytime the discount field changes. Furthermore, method attributing is shown with the RuleWriteAttribute on the CalculateTotal method. This causes impacted rules (ErrorTotalRule) to be reevaluated whenever the method gets executed.

The last rule added is one that detects for errors (in this case, Total less than 0). If this occurs, the policy execution is halted.

Finally, Console.Writeline calls are added as actions to each rule to provide more visibility to rule execution, while also showing that it is possible to access static methods on referenced types. You could also use tracking to get visibility into the rules that are executed (see the RuleActionTrackingEvent sample for an example).

The rules used in this sample are:

ResidentialDiscountRule:

IF OrderValue > 500 AND CustomerType = Residential

THEN Discount = 5%

BusinessDiscountRule:

IF OrderValue > 10000 AND CustomerType = Business

THEN Discount = 10%

HighValueDiscountRule:

IF OrderValue > 20000

THEN Discount = 15%

TotalRule:

IF Discount > 0

THEN CalculateTotal(OrderValue, Discount)

ELSE Total = OrderValue

ErrorTotalRule:

IF Total < 0

THEN Error = "Fired ErrorTotalRule"; Halt

Rule evaluation and execution can also be seen via tracing and tracking. For more information, see RuleActionTrackingEvent Sample.

To build the sample

  1. Download the sample by clicking Download Sample in this topic.

    This will extract the sample project to your local hard disk.

  2. Click Start, point to Programs, point to Microsoft Windows SDK, and then click CMD Shell.

  3. Go to the source directory of the sample.

  4. At the command prompt, type MSBUILD <Solution file name>.

To run the sample

  1. In the SDK Command Prompt window, run the .exe file in the AdvancedPolicy\bin\debug folder (or the AdvancedPolicy \bin folder for the Visual Basic version of the sample), which is located below the main folder for the sample.

See Also

Reference

RuleSet
PolicyActivity

Other Resources

Simple Policy Sample
Tutorial: Use Rules and Conditions
Using the PolicyActivity Activity
Activities Samples
Windows Workflow Foundation Samples

© 2007 Microsoft Corporation. All rights reserved.