NRules结合LINQ的规则反序列化问题及动态数据库规则管理解决方案咨询
NRules结合LINQ的规则反序列化问题及动态数据库规则管理解决方案咨询
最近我在折腾NRules的动态规则管理,想把要校验的规则存在数据库里,动态读取加载,所以得把规则转成JSON格式。我先拿NRules官网入门教程里的那个带LINQ Count 方法的交通违规附加费规则做测试,先贴一下原规则的C#代码:
public class TrafficViolationSurchargeRule : Rule { public override void Define() { InsuranceQuote quote = default!; IEnumerable<TrafficViolation> violations = default!; int totalViolations = 0; When() .Match(() => quote) .Query(() => violations, q => q .Match<TrafficViolation>() .Where(v => v.Driver == quote.Driver, v => v.ViolationType != "Parking", v => v.Date >= DateTime.Now.AddYears(-2)) .Collect()) .Let(() => totalViolations, () => violations.Count()) .Having(() => totalViolations > 1); Then() .Do(ctx => quote.ApplySurcharge(20 * totalViolations)); } }
把这个规则序列化到JSON格式完全没遇到问题,生成的JSON输出如下(注:内容粘贴时出现截断,原本应包含完整的常量值):
{ "name": "Rules.TrafficViolationSurchargeRule", "properties": [ { "name": "Fluent::ClrType", "type": "System.Type", "value": "Rules.TrafficViolationSurchargeRule, Rules, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" } ], "leftHandSide": { "elementType": "And", "childElements": [ { "elementType": "Pattern", "name": "quote", "type": "Domain.InsuranceQuote, Domain, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" }, { "elementType": "Pattern", "name": "violations", "type": "System.Collections.Generic.IEnumerable\u00601[[Domain.TrafficViolation, Domain, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]", "source": { "elementType": "Aggregate", "name": "Collect", "resultType": "System.Collections.Generic.IEnumerable\u00601[[Domain.TrafficViolation, Domain, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]", "source": { "elementType": "Pattern", "name": "$this$", "type": "Domain.TrafficViolation, Domain, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null", "expressions": [ { "name": "Condition", "expression": { "nodeType": "Lambda", "parameters": [ { "name": "$this$", "type": "Domain.TrafficViolation, Domain, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" }, { "name": "quote", "type": "Domain.InsuranceQuote, Domain, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" } ], "body": { "nodeType": "Equal", "left": { "nodeType": "MemberAccess", "memberType": "Property", "name": "Driver", "declaringType": "Domain.TrafficViolation, Domain, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null", "expression": { "nodeType": "Parameter", "name": "$this$", "type": "Domain.TrafficViolation, Domain, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" } }, "right": { "nodeType": "MemberAccess", "memberType": "Property", "name": "Driver", "declaringType": "Domain.InsuranceQuote, Domain, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null", "expression": { "nodeType": "Parameter", "name": "quote", "type": "Domain.InsuranceQuote, Domain, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" } } } } }, { "name": "Condition", "expression": { "nodeType": "Lambda", "parameters": [ { "name": "$this$", "type": "Domain.TrafficViolation, Domain, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" } ], "body": { "nodeType": "NotEqual", "left": { "nodeType": "MemberAccess", "memberType": "Property", "name": "ViolationType", "declaringType": "Domain.TrafficViolation, Domain, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null", "expression": { "nodeType": "Parameter", "name": "$this$", "type": "Domain.TrafficViolation, Domain, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" } }, "right": { "nodeType": "Constant", "type": "string", "value":
内容来源于stack exchange
相关产品推荐
相关产品推荐

