Evaluating Business Rules

The Code Effects evaluation engine is an extremely fast XML-based business rules engine that is capable of evaluating millions of source objects against a rule in a matter of milliseconds. There are two ways to evaluate business rules with the Code Effects engine, and two classes that you can use to do so.

You can evaluate your rules in .NET code by using either one of the Evaluator classes or one of Code Effects' extension methods. The Rule Editor is not necessary during the rule evaluation.

  • Instance. This evaluation method is great if you have a collection of source objects of the same type and need to know the result of the evaluation of each member in the collection against the same rule. Or, use it if you simply need to filter out invalid objects from the collection based on a single rule. Use any of the evaluation classes (outlined below) to evaluate your source(s) in this way. For example, you can create a new instance of Evaluator<TSource> class and call its Evaluate method for each source object:

    Evaluator<SourceObjectType> evaluator =
    	new Evaluator<SourceObjectType>(ruleXmlString);
     
    List<SourceObjectType> list = YourProject.GetSourceObjects();
     
    foreach(SourceObjectType source in list)
    	bool success = evaluator.Evaluate(source);

    The constructor of the Evaluator<T> class used here accepts Rule XML, which can contain a single rule or a large ruleset. It's a good practice to combine a number of rules into one ruleset and pass it to the constructor of the Evaluator class. That way the Evaluator compiles all rules before it starts the iteration through all of the source objects, which saves time. The Evaluate method takes an instance of a source object and returns a System.Boolean that indicates whether the evaluation of the instance against the rule was successful. For execution type rules, this Boolean indicates whether any of the rule sections evaluated to True during the rule evaluation.

    Filtering those source objects against the same rule makes sense if you don't care about the result of each evaluation, and just need to filter out those objects that didn't pass through the rule.

  • Extension. This method is great when you have only one instance of a source and need to quickly evaluate it against a rule, or if you have a collection of sources but one or more of them needs to go through a different rule:

    bool success = sourceInstance.Evaluate(ruleXmlString);

    The RuleExtensions class declares several overloads of the Evaluate extension method. Read its topic for details.

Both of these methods use the same speedy Code Effects evaluation engine; they just supply all necessary parameters differently.

Besides extension methods, Code Effects solution provides two evaluation classes, each serving a slightly different purpose:

  • CodeEffects.Rule.Core.Evaluator<TSource>. Use this class when you know the type of your source object at design-time. It's the fastest among the two classes (the difference between "the fastest" and "the regular" class for a single source object measures in a few nanoseconds). It's simple to use, and if we have a business rule XML document retrieved from storage as a string, we can simply initiate this class and call its Evaluate method:

    // Retrieve Rule XML from your storage
    string ruleXml = MyRuleStorage.GetRule();
    
    // Init the Evaluator class, passing it the Rule XML
    Evaluator<MySourceObject> evaluator = new Evaluator<MySourceObject>(ruleXml);
    
    // Evaluate instance of your source object against the rule
    bool success = evaluator.Evaluate(sourceObjectInstance);

    You can see that this code is similar to the example at the beginning of this topic except that here we just evaluate a single source object instead of looping through a collection. This class can also be used to evaluate a base class and its descendant types against the same rule. Read this topic for details.

  • CodeEffects.Rule.Core.Evaluator. This is a plain class that is useful in situations where you generate assemblies and/or types dynamically, and might not know the objects’ types until run-time. Use it if you employ our FlexSource technology to build your sources. This class accepts the source objects' type or an IFlexDataProvider as a parameter in its constructor:

    Evaluator evaluator = new Evaluator(typeof(MySourceObject), ruleXmlString);
    bool success = evaluator.Evaluate(sourceObjectInstance);

    With this class, you get more freedom with the small trade-off of slightly slower performance. As with the generic Evaluator class, this class can be used to evaluate the base-child source combinations, too.

Because the business needs of organizations and industries can have unforeseeable differences, Code Effects component attempts to be as forgiving as possible, allowing developers to use many kinds of scenarios, and throwing exceptions only when the result of rule evaluation is clearly unpredictable. For example, it allows comparisons of different .NET numeric types without asking developers for explicit boxing or type conversion. It also provides internal type conversion between nullable and regular types when preparing the source for evaluation.

Comments: 0
Post your questions about Code Effects on Stackoverflow.com
and become a part of our growing community
Name (optional):
Comment (URLs are allowed and must start with http:// or https://; all tags will be encoded):
Remaining character count: