RulesEngine
判定规则是匹配失败还是匹配异常
当RulesEngine进行规则匹配的是,不论是失败还是异常,IsSuccess都为false,且都不抛出异常
- 失败:指规则不匹配
- 异常:指给到的参数或者规则本身出异常
看下结果类
public class RuleResultTree
{
public Rule Rule { get; set; }
public bool IsSuccess { get; set; }
public IEnumerable<RuleResultTree> ChildResults { get; set; }
public Dictionary<string, object> Inputs { get; set; }
public ActionResult ActionResult { get; set; }
public string ExceptionMessage { get; set; }
}
这里可以通过ExceptionMessage是否有值来判定是否是异常
不要使用 Dictionary<string,object>
Exception while parsing expression `customer.HalfYearOrders > 3` -The binary operator GreaterThan is not defined for the types 'System.Object' and 'System.Int32'.
如上所示,会提示无法比较
建议:
如果类型是确定的,使用类对象或者匿名对象
var data = new { Name: "张三"};
如果不确定的,使用ExpandoObject
IDictionary<string, object> data = new System.Dynamic.ExpandoObject();
dapper 动态数据的使用
使用dapper查出dynamic数据,如下代码
dynamic obj = db.QuerySingle("ZJX_usp_xx", new { cardNo = CardNo }, commandType: System.Data.CommandType.StoredProcedure);
如果将obj直接用于param,将提示如下错误
var engine= new RulesEngine.RulesEngine(workflowRules);
var workflowParam = new RulesEngine.Models.RuleParameter("customer", obj);
var resultList = await engine.ExecuteAllRulesAsync('workflowName', workflowParam);
这里 resultList 会提示
Exception while parsing expression `customer.HalfYearOrders>3` - No property or field 'HalfYearOrders' exists in type 'DapperRow'
解决办法是将dynamic数据取出来放在ExpandoObject中,然后再用
IDictionary<string, object> dapperRowProperties = obj as IDictionary<string, object>;
IDictionary<string, object> expando = new System.Dynamic.ExpandoObject();
foreach (KeyValuePair<string, object> property in dapperRowProperties)
expando.Add(property.Key, property.Value);
var engine= new RulesEngine.RulesEngine(workflowRules);
var workflowParam = new RulesEngine.Models.RuleParameter("customer", obj);
var resultList = await engine.ExecuteAllRulesAsync('workflowName', expando); // 注意这里,使用转换过的