WF4.0 Beta1中的规则引擎变化

在WF3.x时代我们可以使用声明性的条件和代码方式的条件,也可以使用支持正向链接的RuleSet。当然我们还可以使用基于CodeDOM的API来用代码的方式声明。

创新互联公司坚持“要么做到,要么别承诺”的工作理念,服务领域包括:成都网站建设、成都网站制作、企业官网、英文网站、手机端网站、网站推广等服务,满足客户于互联网时代的盐亭网站设计、移动媒体设计的需求,帮助企业找到有效的互联网解决方案。努力成为您成熟可靠的网络建设合作伙伴!

在微软刚刚发布的WF4.0 Beta1中我们已经看不到这些了,WF4.0提供了统一的完全声明式的表达式(Expression)。现在的版本只支持使用VB来构建表达式(Expression),但是在设计上是可以支持任何语言的,微软也会在未来的版本中提供对其他语言的支持。

WF4.0中表达式是ValueExpression类型的,我们在VS中构造表达式的窗口是ExpressionTextBox类的实例,它也是可以再外部重新宿主的,只不过只有和VS结合的时候才有智能感知和颜色的支持。在表达式中我们可以引用工作流中的变量和参数。这些信息都会被序列化到XAML中。提供了表达式(Expression)并不是就不要原来的方式,微软在开发WF4.0一个很重要的部分就是对WF3.x全面兼容。在WF4.0中提供了一个Interop活动可以帮助我们很好的完成现有WF3.x程序的迁移,我们只需要简单的设置它的Body Type属性即可,我们可以将WF4.0中的变量和参数绑定到WF3.x中的依赖属性上,如下图:

  

在WF4.0 beta1中没有提供对正向链接的RuleSet功能,官方已经声明会在将来的版本中加大这部分的投入。现在如果我们要想在WF4.0 Beta1使用类似的功能我们可以开发一个自定义活动来完成,下面的例子来源于WF Samples中,首先是活动的代码部分:

 
 
 
 
  1. namespace Microsoft.Samples.Rules  
  2. {  
  3.     using System;  
  4.     using System.Activities;  
  5.     using System.ComponentModel;  
  6.     using System.Workflow.Activities.Rules;  
  7.     using System.Workflow.ComponentModel.Compiler;  
  8.  
  9.     [Designer(typeof(Microsoft.Samples.Rules.PolicyDesigner))]  
  10.     public sealed class Policy40Activity : NativeActivity  
  11.     {  
  12.         public RuleSet RuleSet { get; set; }  
  13.  
  14.         [IsRequired]  
  15.         public InOutArgument TargetObject { get; set; }  
  16.         public OutArgument ValidationErrors { get; set; }  
  17.  
  18.         protected override void OnOpen(DeclaredEnvironment environment)  
  19.         {  
  20.             if (this.RuleSet == null)  
  21.             {  
  22.                 throw new System.ArgumentNullException("RuleSet property can't be null");  
  23.             }  
  24.         }  
  25.  
  26.         protected override void Execute(ActivityExecutionContext context)  
  27.         {  
  28.             // validate before running  
  29.             Type targetType = this.TargetObject.Get(context).GetType();  
  30.             RuleValidation validation = new RuleValidation(targetType, null);  
  31.             if (!this.RuleSet.Validate(validation))  
  32.             {  
  33.                 // set the validation error out argument  
  34.                 this.ValidationErrors.Set(context, validation.Errors);  
  35.                 // throw a validation exception  
  36.                 throw new ValidationException(string.Format("The ruleset is not valid. {0} validation errors                   found (check the ValidationErrors property for more information).", validation.Errors.Count));  
  37.             }  
  38.             // execute the ruleset  
  39.             object evaluatedTarget = this.TargetObject.Get(context);  
  40.             RuleEngine engine = new RuleEngine(this.RuleSet, validation);  
  41.             engine.Execute(evaluatedTarget);  
  42.             // update the target object  
  43.             this.TargetObject.Set(context, evaluatedTarget);  
  44.         }  
  45.     }  

下面是活动的设计器部分,在WF4.0中提供了对活动设计器的可视化支持:

 
 
 
 
  1.  x:Class="Microsoft.Samples.Rules.PolicyDesigner" 
  2.     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
  3.     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
  4.     xmlns:sad="clr-namespace:System.Activities.Design;assembly=System.Activities.Design" 
  5.     xmlns:sadv="clr-namespace:System.Activities.Design.View;assembly=System.Activities.Design"> 
  6.      
  7.          x:Uid="sadv:ArgumentToExpressionConverter_1"          x:Key="argumentToExpressionConverter" /> 
  8.      
  9.      
  10.          
  11.              x:Uid="RowDefinition_1" /> 
  12.              x:Uid="RowDefinition_2" /> 
  13.          
  14.          
  15.              x:Uid="ColumnDefinition_1" Width="70*"  /> 
  16.              x:Uid="ColumnDefinition_2" Width="196*" /> 
  17.          
  18.          Content="Target Object" Name="label1" Margin="0,5,0,7"/> 
  19.            
  20.             x:Uid="ExpressionTextBox_1"   
  21.             Grid.Row="0" Grid.Column="1"   
  22.             AutomationProperties.AutomationId="TargetObject"              
  23.             Width="190" Margin="9,7,9,7" MaxLines="1"   
  24.             VerticalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="Auto"   
  25.             Expression="{Binding Path=ModelItem.TargetObject, Mode=TwoWay, Converter={StaticResource                          argumentToExpressionConverter}, ConverterParameter=InOut}" 
  26.             UseLocationExpression="True" 
  27.             OwnerActivity="{Binding Path=ModelItem, Mode=TwoWay}" /> 
  28.          Content="Edit RuleSet" Name="button1" Width="190" Margin="9,9,9,9" Click="button1_Click"          Grid.Row="1" Grid.Column="1" /> 
  29.      
  30.  

效果如下图:

下面是当点击按钮后,会出现RuleSet的编辑器:

 
 
 
 
  1. using System;  
  2. using System.Activities;  
  3. using System.Windows;  
  4. using System.Windows.Forms;  
  5. using System.Workflow.Activities.Rules;  
  6. using System.Workflow.Activities.Rules.Design;  
  7.  
  8. namespace Microsoft.Samples.Rules  
  9. {  
  10.     // Interaction logic for PolicyDesigner.xaml  
  11.     public partial class PolicyDesigner  
  12.     {  
  13.         public PolicyDesigner()  
  14.         {  
  15.             InitializeComponent();  
  16.         }  
  17.  
  18.         private void button1_Click(object sender, RoutedEventArgs e)  
  19.         {  
  20.             // verifiy that TargetObject property has been configured  
  21.             object targetObject = ModelItem.Properties["TargetObject"].ComputedValue;  
  22.             if (targetObject == null)  
  23.             {  
  24.                 System.Windows.MessageBox.Show("TargetObject needs to be configured before adding the rules");  
  25.                 return;  
  26.             }  
  27.             // verify that target object is correctly configured  
  28.             InOutArgument arg = targetObject as InOutArgument;  
  29.             if (arg == null)  
  30.             {  
  31.                 System.Windows.MessageBox.Show("Invalid target object");  
  32.                 return;  
  33.             }  
  34.             // open the ruleset editor  
  35.             Type targetObjectType = arg.ArgumentType;  
  36.             RuleSet ruleSet = ModelItem.Properties["RuleSet"].ComputedValue as RuleSet;  
  37.             if (ruleSet == null)  
  38.                 ruleSet = new RuleSet();  
  39.             RuleSetDialog ruleSetDialog = new RuleSetDialog(targetObjectType, null, ruleSet);  
  40.             DialogResult result = ruleSetDialog.ShowDialog();  
  41.             // update the model item  
  42.             if (result == DialogResult.OK)             {  
  43.                 ModelItem.Properties["RuleSet"].SetValue(ruleSetDialog.RuleSet);  
  44.             }  
  45.         }  
  46.     }  

这样我们就可以再WF4.0中使用该活动了,如下图:

【编辑推荐】

  1. 浅谈WF 4.0 Beta1中的跟踪机制
  2. 微软MVP初探WF 4.0 beta1 崭新面貌让人吃惊
  3. 详解在Workflow工作流中如何使用角色
  4. 详解工作流架构与实现
  5. 用UML描述工作流管理

文章题目:WF4.0 Beta1中的规则引擎变化
本文来源:http://www.shufengxianlan.com/qtweb/news33/96833.html

网站建设、网络推广公司-创新互联,是专注品牌与效果的网站制作,网络营销seo公司;服务项目有等

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联