Navigation

Categories
Show Navigation Next Topic  »

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, Evaluator 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, use our FlexSource technology.


Post your questions on Stackoverflow and become a part of our growing community

Comments: 0