DynamicEvaluator class

Namespace: CodeEffects.Rule.Core
Assembly: CodeEffects.Rule.dll

Provides the core functionality for evaluation of business rules without knowing the exact type of the source object at design time. Read the rules evaluation topic for details.

Syntax

public class DynamicEvaluator

Constructors

  • DynamicEvaluator(System.String, GetRuleDelegate = null)
    Parameters:
    • rulesetXml, System.String - This string can contain either a single Rule XML document, or a single <rule> node that contains a rule, or a collection of rules called the ruleset.
    • getRule, CodeEffects.Rule.Core.GetRuleDelegate - This nullable parameter takes a reference to a method that returns string Rule XML by rule ID. Pass this delegate if your rule may potentially reference reusable rules that are not stored in the same XML ruleset.

Methods

  • Evaluate(System.Object, System.String = null) Returns: System.Boolean
    Parameters:
    • source, System.Object - Instance of the source object.
    • ruleId, System.String - If the Rule XML passed to the constructor of this class is a ruleset then this nullable parameter contains the ID of the particular rule located inside of that ruleset. The DynamicEvaluator uses this ID to retrieve the rule from the ruleset and evaluate the source against it.

    For evaluation type rules, returns the result of the evaluation. For execution type rules, returns True if any rule action was invoked during the evaluation. Otherwise, returns False.

Notes

This class is ideal for situations where the exact type of the source object is not known until the moment it's generated, filled with data and sent for evaluation against a business rule(s). Unlike other Code Effects evaluators, the DynamicEvaluator class does not compile the rule in the constructor. Instead, it maintains a dictionary of evaluators based on a type of objects encountered during evaluation.

During evaluation the DynamicEvaluator assumes that the rule is valid for a given source object. It checks the dictionary for an existing evaluator that matches the type of the object and creates one if necessary. It tries to evaluate each condition, canceling the execution if an exception arises. Set the value of the PersistTypeNameInRuleXml property of the SourceAttribute to False for source objects that are going to be evaluated using the DynamicEvaluator class. This class provides a higher level of flexibility but incurs slight performance penalty because of that.

Example

DynamicEvaluator evaluator = new DynamicEvaluator(ruleXml);
bool success = evaluator.Evaluate(sourceObjectInstance);

The code sample above demonstrates that the type of the sourceObjectInstance is not known in all stages of the rule evaluation.

Post your support requests on Stackoverflow.com. You can also post your comments and product feedback using the form at the bottom of this page.
Comments: 0
Name (optional):
Comment (URLs are allowed and must start with http:// or https://; all tags will be encoded):
Remaining character count: