2

我想做这样的事情

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);

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

这种方法有什么潜在的陷阱???

提前致谢

4

3 回答 3

2

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

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

于 2009-10-07T03:30:57.790 回答
2

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

您的解决方案在您编译之前一直很好,然后根据您正在寻找运行时灵活性的评论声音进行设置和锁定。

于 2009-10-07T04:29:18.310 回答
2

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

于 2009-10-07T04:37:52.083 回答