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

发布于 2024-08-07 03:00:36 字数 766 浏览 6 评论 0原文

我想做这样的事情

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

只是想在我深入并淹没之前问一下。

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

提前致谢

I am looking to do something like this

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){}
}

Then in client code use it as

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

OR

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

productBiz.ApplyRules(rulesetDelegate);

Just wanted to ask before i dive deep and drown.

What are the potential pitfalls with this approach???

Thanks in advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

调妓 2024-08-14 03:00:36

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

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

I'm not sure what you mean by possible. It's certainly possible to write a rules engine in this way and you've demo'd an outline of how to achieve this.

Don't forget that extension methods are just syntactic sugar on top of static methods. Asking if you can do X type of programming with extension methods is no different than asking if you can do X type of programming with static methods. Static methods may not look as nice but they are just as powerful.

花开雨落又逢春i 2024-08-14 03:00:36

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

您的解决方案在编译之前一直很好,然后它被设置并锁定,从您正在寻找运行时灵活性的评论的声音来看。

If you're looking at changing the rules at run-time then you might want to consider something more like MEF or similar.

Your solution is fine up until you compile, then it's set and locked, from the sound of your comments you're looking for run-time flexibility.

淡淡的优雅 2024-08-14 03:00:36

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

Look at the implementation of business rules in CSLA http://lhotka.net/ . In that you define a rule w/ a particular signature, and add it into the object's rule store, either at a class level or instance level. The syntax of what you are attempting to do is off-putting, but the method (defining business rules via static methods which are executed at run time) is exactly what CSLA does.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文