Engine Implementation
The Code Effects business rules engine is a .NET Standard 2.0 assembly that supports all current .NET frameworks, including .NET 4.6.2+ and .NET 8.0+, and can be used in any .NET project.
- If you are using the Engine Free edition, reference the latest version of the
CodeEffects.Rule.Engine
assembly from NuGet.
- For all other editions, obtain the
CodeEffects.Rule.Engine.Enterprise
and CodeEffects.Rule.Common
assemblies from Code Effects Software and reference them in your project.
Compile and run your project to make sure everything works correctly.
Next, create an instance of the Evaluator
class, passing it your RuleXML. This compiles your business rule into IL object(s). Then evaluate that rule against your source by invoking the Evaluate()
method. That’s all it takes to evaluate a rule.
Here is a basic C# example:
using System;
using E = CodeEffects.Rule.Engine;
namespace MyProject
{
// The source object
public class MySource
{
public int ID { get; set; } = 0;
public string Name { get; set; }
}
public class MyClass
{
public bool Test(MySource dataToEvaluate)
{
// Get the rule you want to use to evaluate the data
var ruleXMLString = YourRuleStorage.GetYourRule();
// Create an instance of the Evaluator class
var ev = new E.Evaluator<MySource>(ruleXMLString);
// Evaluate the rule
var success = ev.Evaluate(dataToEvaluate);
}
}
}
Engine Subscription Edition
If you have the Engine Subscription edition of the engine, your project must maintain a special license text file codeeffects.ce
that needs to be renewed every 6 (six) month without needing to recompile or redeploy your project. This file is included in the package that you get from Code Effects together with your product key and .NET assemblies.
Save the codeeffects.ce
file anywhere on the machine or VM that runs your project and set the value of Evaluator.LicenseFileFullPath
property to its full physical path BEFORE any Code Effects code:
CodeEffects.Rule.Engine.Evaluator.LicenseFileFullPath = "C:\\FullPath\\codeeffects.ce";
To renew this file, log in to the Code Effecst Portal using your product key, and click the License File icon to the right of your license record. This will download the file to your drive.
Our system will attempt to notify you when your license is about to expire by sending an email to all addresses we have on record; however, we cannot guarantee that this notification will be delivered.
If the current license file in your project has expired, the Evaluator
class adds a three-second delay to all invocations of the Evaliate()
and Filter()
methods, effectively turning your assembly into the Engine Free edition until the license file is renewed. It throws an InvalidOperationException
if the license file is missing or corrupt.
Performance Tips
Rulesets
The Code Effects rules engine is extremely fast. We have demonstrated evaluating an array of ONE MILLION randomly filled objects against a one-condition evaluation type rule in a single thread in just 15 millionths of a second (0.000015 sec).
Even though performance is excellent, it still makes sense to optimize your evaluations where possible.
The most expensive operation is compiling your rule(s) during initialization of the Evaluator
class. If you need to evaluate multiple rules against the same data, you can combine them into a single ruleset by inserting them into an empty XML document and passing that ruleset as your RuleXML to the Evaluator
constructor:
<?xml version="1.0" encoding="utf-8"?>
<codeeffects
xmlns="https://codeeffects.com/schemas/rule/41"
xmlns:ui="https://codeeffects.com/schemas/ui/4">
<rule id="ID1" ...>
<!-- Rule #1 -->
</rule>
<rule id="ID2" ...>
<!-- Rule #2 -->
</rule>
<rule id="ID3" ...>
<!-- Rule #3 -->
</rule>
...
</codeeffects>
Evaluator Instances
If at all possible, reuse Evaluator
instances to run multiple data sets against the same rules. Each instance already holds the compiled rules, and recompilations are expensive, so caching and reusing one avoids unnecessary overhead.
Resources