Search code examples
c#business-objectsrule-enginebusiness-logic-layerbusiness-rules

Can we use Extension Methods for building business rule engines?


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


Solution

  • 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.