Rule Evaluation and Object Inheritance

The Code Effects business rules engine allows evaluation of descendant source objects. To illustrate this concept, let's create one base class and two classes that inherit from the base:

public class MyBase
{
	public int ID { getset; }
	public string Name { getset; }
 
	public MyBase() { }
}
 
public class MySourceOne : MyBase
{
	public DateTime Date { getset; }
 
	public MySourceOne() { }
}
 
public class MySourceTwo : MyBase
{
	public TimeSpan Time { getset; }
 
	public MySourceTwo() { }
}

Let's also assume that we have an evaluation type rule that uses the base class' fields, like this:

Check if ID is greater than [0] and Name starts with "a"

If we define instances of all three classes, Code Effects component will allow us to evaluate each instance against our rule, because the rule only uses fields that are declared in the base class, and that are therefore available to any of its descendant classes:

MyBase zero = new MyBase { ID = 0, Name = "abc" };
MySourceOne one = new MySourceOne { ID = 1, Name = "xyz" };
MySourceTwo two = new MySourceTwo { ID = 2, Name = "abc" };
 
// Retrieve Rule XML from your storage
string ruleXml = GetRuleXmlFromSomewhere();
 
Evaluator evaluator = new Evaluator(typeof(MyBase), ruleXml);
 
bool success = evaluator.Evaluate(zero); // Returns False
success = evaluator.Evaluate(one); // Returns False
success = evaluator.Evaluate(two); // Returns True

Notice that we created an instance of the Evaluator class using the type of our base class, but we were able to pass instances of all three of our classes for evaluation without receiving any exceptions. If you build your sources dynamically, and their types are available only at the last moment, use the DynamicEvaluator which can evaluate your sources without checking their types at all.

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: