public class AllowAtleastOneCountryRule : Rule { public override void Define() { Profile profile = null; string str = @"At least one country has to be defined as 'permitted'"; bool enabled = AllRules.GetDict()[str];//Checks if the rule is enabled When() .Match<FundProfile>(() => productProfile) .Exists<FundProfile>(p => enabled,p => RuleViolation(p)); Then() .Do(_ => profile .displayError(str)); } bool RuleViolation(FundProfile pp) { try { if (pp.DefaultMode.Equals(Helper.DefaultModes.Allow.ToString())) { if (pp.listofCountries.Count < pp.TotalCountries)//Okay return false; else//Rule violation return true; } else//Deny { if (pp.listofCountries.Count > 0)//Okay return false; else//Rule violation return true; } } catch(Exception e) { throw new InvalidRuleException(e.Message); } } }
正如您所看到的,我正在使用规则来调用另一种方法来评估几个条件.我觉得我没有在这里使用Rete算法的全部功能,因为我正在为自己预先评估事物.
任何人都可以指导我如何处理这个问题?
解决方法
您的代码看起来很好,您有一个复杂的规则并且您将其封装起来.
使用.Query而不是.Exists实现complex logic,将封装的逻辑转换为linq或lambda表达式.
然后应用DSL Extension,使您的代码更具可读性.
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。