【问题标题】:Can we use Extension Methods for building business rule engines?我们可以使用扩展方法来构建业务规则引擎吗?
【发布时间】:2010-12-04 11:55:51
【问题描述】:

我想做这样的事情

public class ProductBiz: BizBase<Product> {

public List<String> BrokenRules {get;set;}

// Some kind of data + biz operation implementation

}

public static class ProductBizExtensions{

public ProductBiz Rule1(this ProductBiz prodBiz)
{}
public ProductBiz Rule2(this ProductBiz prodBiz)
{}

public bool ApplyRules (this ProductBiz prodBiz, Func<ProductBiz,bool> ruleset){}
}

然后在客户端代码中使用它

productBiz.Rule1().Rule2();
productBiz.Rule2().Rule1();

或

// create multicasted delegate of type Func<ProductBiz,bool> say rulesetDelegate

productBiz.ApplyRules(rulesetDelegate);

只是想在我深潜淹死之前问一下。

这种方法的潜在缺陷是什么???

提前致谢

【问题讨论】:

  • 您的第一个代码块中是否有错字?第二个Rule1 应该是Rule2 吗?

标签: c# business-objects rule-engine business-logic-layer business-rules


【解决方案1】:

我不确定你所说的可能是什么意思。以这种方式编写规则引擎当然是可能的,并且您已经演示了如何实现这一点的大纲。

不要忘记扩展方法只是静态方法之上的语法糖。询问您是否可以使用扩展方法进行 X 类型的编程与询问您是否可以使用静态方法进行 X 类型的编程没有什么不同。静态方法可能看起来不那么好,但它们同样强大。

【讨论】:

    【解决方案2】:

    如果您正在考虑在运行时更改规则,那么您可能需要考虑更像MEF 或类似的东西。

    在您编译之前,您的解决方案都很好,然后根据您正在寻找运行时灵活性的 cmets 的声音设置并锁定它。

    【讨论】:

    • 我有准备好的产品版本吗?我刚刚在网站上看到了预览。不过好主意
    【解决方案3】:

    查看 CSLA http://lhotka.net/ 中业务规则的实现。在这种情况下,您可以定义带有特定签名的规则,并将其添加到对象的规则存储中,无论是在类级别还是实例级别。您尝试执行的操作的语法令人反感,但方法(通过在运行时执行的静态方法定义业务规则)正是 CSLA 所做的。

    【讨论】:

      猜你喜欢
      • 2011-03-17
      • 2010-10-21
      • 1970-01-01
      • 2013-01-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-07-15
      相关资源
      最近更新 更多