C# 如何设计规则引擎?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/93147/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 11:35:45  来源:igfitidea点击:

How to design a rule engine?

提问by

I'm supposed to create a simple rule engine in C#. Any leads on how I can proceed?. It's a minimalistic rule engine, and would use SQL server as the back end. Do we have any general blueprint or design patterns that generally apply to rule engines? What kind of .Net technologies can I use to design one? Any directions would be helpful. Thanks.

我应该在 C# 中创建一个简单的规则引擎。关于我如何进行的任何线索?它是一个简约的规则引擎,并将使用 SQL 服务器作为后端。我们是否有任何通常适用于规则引擎的通用蓝图或设计模式?我可以使用什么样的 .Net 技术来设计一个?任何方向都会有所帮助。谢谢。

采纳答案by Tom Kidd

If you're using .NET 3.0 or later, you can use the Rules Engine of Windows Workflow Foundationwithout having to acutally use Workflow.

如果您使用 .NET 3.0 或更高版本,则可以使用 Windows Workflow Foundation 的规则引擎,而无需实际使用 Workflow。

I've done this on a project, and you can use SQL or XML as the backend, and it works great. You can use the IDE that comes with the Workflow examples and put it in your own apps. It's excellent.

我在一个项目上做过这个,你可以使用 SQL 或 XML 作为后端,而且效果很好。您可以使用 Workflow 示例附带的 IDE 并将其放入您自己的应用程序中。它很棒。

回答by SpoiledTechie.com

What kind of Rule engine you looking for? For styling practices? If so, go check out StyleCop. Not the answer, but there might already be something out there for you.

您正在寻找什么样的规则引擎?对于造型实践?如果是这样,请查看StyleCop。不是答案,但可能已经有适合您的东西。

回答by David Starr - Elegant Code

  1. I cannot believe you would implement your own considering there are so many available commercially and open source.

  2. I recommend taking a look at InRule as a great commercial option that is reasonable priced, or NxBRE in the open source space.

  1. 考虑到有这么多可用的商业和开源软件,我无法相信您会自己实现。

  2. 我建议将 InRule 视为一种价格合理的绝佳商业选择,或者将其视为开源领域中的 NxBRE。

回答by jwarzech

Are you given any indication on method? (ie if this is supplemented by course material what are you currently learning?) If this is meant to be a fairly basic system you might find success looking into to Deterministic Finite State Machineand Nondeterministic Finite State Machine

你对方法有任何指示吗?(即,如果这由课程材料补充,您目前正在学习什么?)如果这是一个相当基本的系统,您可能会成功地研究确定性有限状态机非确定性有限状态机

回答by JeeBee

If you have business analysts to program the high level rule engine, then fine - pick one of the before-mentioned rule engines or roll your own (including workflows). If not, then just code your business logic in code and if you ever need to hire business analysts and redo the system, you're in a good place to be.

如果您有业务分析师对高级规则引擎进行编程,那么很好 - 选择前面提到的规则引擎之一或推出您自己的规则引擎(包括工作流)。如果没有,那么只需在代码中编写您的业务逻辑,如果您需要聘请业务分析师并重做系统,那么您就处于一个很好的位置。

回答by Arnaud

回答by Dib

If you want to write your implementation something like this...

如果你想写你的实现是这样的......

[TestMethod]
public void GreaterThanRule_WhenGreater_ResultsTrue()
{
    // ARRANGE
    int threshold = 5;
    int actual = 10;

    // ACT
    var integerRule = new IntegerGreaterThanRule();
    integerRule.Initialize(threshold, actual);

    var integerRuleEngine = new RuleEngine<int>();
    integerRuleEngine.Add(integerRule);
    var result = integerRuleEngine.MatchAll();

    // ASSERT
    Assert.IsTrue(result);
}

... or this...

... 或这个...

[TestMethod]
public void GreaterThanRule_WhenGreater_ResultsTrue()
{
    // ARRANGE
    int threshold = 5;
    int actual = 10;

    // ACT
    var integerRule = new IntegerGreaterThanRule(threshold);

    var integerRuleEngine = new RuleEngine<int>();
    integerRuleEngine.ActualValue = actual;
    integerRuleEngine.Add(integerRule);

    // Get the result
    var result = integerRuleEngine.MatchAll();

    // ASSERT
    Assert.IsTrue(result);
}