gpt4 book ai didi

c# - 我们可以使用扩展方法来构建业务规则引擎吗?

转载 作者:行者123 更新时间:2023-12-03 20:26:00 34 4
gpt4 key购买 nike

我想做这样的事情

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

只是想在我潜入深渊之前问一下。

这种方法有哪些潜在的陷阱???

提前致谢

最佳答案

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

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

关于c# - 我们可以使用扩展方法来构建业务规则引擎吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1529283/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com