Can we use extension methods to build business rule engines?
I want 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 like
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 diving deep and drowning.
What are the potential pitfalls with this approach?
Thank you in advance
source to share
I'm not sure what you mean if possible. It is certainly possible to write a rule engine this way, and you have demonstrated a plan for 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 programming with extension methods is no different from asking if you can do X-type programming with static methods. Static methods may not look that good, but they are just as powerful.
source to share
Have a look at the implementation of business rules in CSLA http://lhotka.net/ . In this case, you define a rule with a specific signature and add it to the object's rule store, either at the class level or at the instance level. The syntax for what you are trying to do is disable, but the method (defining business rules with static methods executed at runtime) is exactly what the CSLA does.
source to share