加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 这是在NRules中定义规则的正确方法吗?

发布时间:2020-12-15 20:58:38 所属栏目:百科 来源:网络整理
导读: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 e
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,使您的代码更具可读性.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读