支持可以为 null 的类型
规则引擎支持在业务规则中使用可为 null 的类型。 可以在 .NET 类绑定、XML 绑定和数据库绑定中使用可为空的类型。 目前,业务规则编辑器工具不支持在业务规则中使用可为 null 的类型。 以编程方式创建规则时,可以使用可以为 null 的类型。
在 .NET 类绑定中使用可为空的类型
可以创建对属性或类型为可为 null 类型的字段的类成员绑定。 还可以创建对方法的类成员绑定,该方法采用可为 null 类型的参数和/或返回可为 null 类型的值。 以下示例代码演示如何访问可为空字段,以及如何从业务规则中的方法访问可为 null 类型的返回值。 如果按原样执行控制台应用程序,则会看到 prop 字段的值设置为 5。 如果未初始化 类中的 prop 字段或将其初始化为 null 并运行代码,则会看到 prop 字段的值设置为 1。
using Microsoft.RuleEngine;
namespace UseNullableAsm
{
class Program
{
public class Class1
{
public int? prop = 1;
private int? prop2 = 4;
public int? GetProp2()
{
return prop2;
}
}
static void Main(string[] args)
{
//Create the class binding for the Class1 class
ClassBinding cbCls1 = new ClassBinding(typeof(Class1));
//Create the class member binding to the GetProp2 method of Cls1 class
ClassMemberBinding cmGetProp2 = new ClassMemberBinding("GetProp2", cbCls1);
//Create the class member binding to the to GET the value of prop
ClassMemberBinding cmGetProp = new ClassMemberBinding("prop", cbCls1);
//Create arguments for the prop1 field, which is prop1 + GetProp2()
UserFunction ufGetProp = new UserFunction(cmGetProp);
Add addArg = new Add(ufGetProp, new UserFunction(cmGetProp2));
ArgumentCollection al1 = new ArgumentCollection();
al1.Add(addArg);
//Set the value of prop to prop1 + cmGetPro2()
ClassMemberBinding cmProp = new ClassMemberBinding("prop", cbCls1, al1);
//Create a userfunction based on cmProp and add to the action collection
UserFunction ufProp = new UserFunction(cmProp);
ActionCollection ac = new ActionCollection();
ac.Add(ufProp);
//Create the condition list IF prop == 1
Equal eq = new Equal(ufGetProp, new Constant(1));
//Create the rule
// If (prop == 1)
// Then prop = prop + GetProp2()
Rule rl = new Rule("NullableTestRule", eq, ac);
//Create the condition list IF prop != 1
NotEqual neq = new NotEqual(ufGetProp, new Constant(1));
//Set the value of prop to prop to 1
Constant ct = new Constant(1);
ArgumentCollection al2 = new ArgumentCollection();
al2.Add(ct);
//Create class member binding to prop field with argument value 1
ClassMemberBinding cmSetProp = new ClassMemberBinding("prop", cbCls1, al2);
//Create a userfunction based on cmSetProp and add to the action collection
UserFunction ufSetProp = new UserFunction(cmSetProp);
ActionCollection ac2 = new ActionCollection();
ac2.Add(ufSetProp);
//Create the second rule
// If (prop != 1)
// Then prop = 1
Rule rl2 = new Rule("NullableTestRule2", neq, ac2);
//Create the policy and add both the rules to the policy
RuleSet rs = new RuleSet("NulableTestPolicy");
rs.Rules.Add(rl);
rs.Rules.Add(rl2);
//Create the .NET object fact
Class1 cls1Obj = new Class1();
//Print the value of the field prop before executing the policy
Console.WriteLine("Value of the prop field is " + cls1Obj.prop);
//Execute the policy
PolicyTester pt = new PolicyTester(rs);
pt.Execute(cls1Obj);
//Print the value of the field prop after executing the policy
Console.WriteLine("Value of the prop field is " + cls1Obj.prop);
}
}
}
在数据库绑定中使用可为空的类型
还可以在数据库绑定中使用可为空的类型。 以下示例代码片段演示如何在数据库绑定中使用可为 null 的类型。
DataColumnBinding dcBinding = new DataColumnBinding(“col”, typeof(int?), dbBinding);
假设你有一个规则,条件用于检查数据库列的值,以查看是否等于 3。 如果列的值为 null,则表达式的计算结果为 false。 它不会导致异常。
在 XML 绑定中使用可为空的类型
同样,可以在 XML 绑定中使用可为 null 的类型。 以下示例代码片段演示如何在 XML 绑定中使用可为 null 的类型。
XMLDocumentFieldBinding xfb1 = new XMLDocumentFieldBinding(typeof(int?),"ID",xdb);
反馈
https://aka.ms/ContentUserFeedback。
即将发布:在整个 2024 年,我们将逐步淘汰作为内容反馈机制的“GitHub 问题”,并将其取代为新的反馈系统。 有关详细信息,请参阅:提交和查看相关反馈