开发者

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
开发者_如何学C
}

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


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.


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜