Engine Implementation
The Code Effects business rules engine is a .NET Standard 2.0 assembly that runs on virtually any platform supporting the .NET runtime, including Windows, Linux, macOS, Docker containers, mobile platforms and cloud environments. It supports .NET Framework 4.6.2 and later and .NET Core 2.0 and later, including .NET 8 and later, has no third-party dependencies, and can be integrated into virtually any application.
Estimated Time to Production
A stand-alone implementation of the Rule Engine and the Rule Editor together, with your own rule storage, versioning and testing, typically takes:
- A team of two or three .NET developers: 3 to 6 months.
- An AI coding agent: 5 to 10 days, plus testing and review by your team.
With the Rule Repository, which provides the storage, versioning, testing and approval workflow, the whole implementation, including evaluation and Audit, takes one junior .NET developer 2 to 3 days, or an AI coding agent 1 to 2 hours, plus 1 to 2 days of testing. These are typical figures, not a commitment; see Code Effects Implementation for what they assume.
Steps
Step 1
Reference the Rule Engine in your project:
- Under Code Effects Personal, free for personal, community, development and research use, reference the latest version of
CodeEffects.Rule.Engine.Personal from NuGet. It also installs CodeEffects.Rule.Common.Personal.
- For commercial use, with a Code Effects package (Standard or above), download your package with your product key from the Code Effects Portal and reference
CodeEffects.Rule.Engine and CodeEffects.Rule.Common from it. See Code Effects Packages.
Step 2
Use the Evaluator class to evaluate your rules against your source object(s). Here is a simple example:
using 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 ruleXml = YourRuleStorage.GetYourRuleXml();
// Create an instance of the Evaluator class
var evaluator = new Evaluator<MySource>(ruleXml);
// Evaluate the rule
return evaluator.Evaluate(dataToEvaluate);
}
}
}
This example uses the generic Evaluator class with default options and settings. Depending on your implementation, you may prefer the non-generic Evaluator class or one of the Evaluate() extension method overloads. Passing a rule or ruleset to the Evaluator constructor compiles it into IL, so create the evaluator once and reuse it to evaluate any number of source objects.
Use the Filter() extension method if your application processes collections of source objects and you only need to keep the items that satisfy an evaluation type rule. See Data Filtering.
For additional guidance on optimizing rule evaluation, see the Evaluation Performance Tips page.
Step 3
Load the rules your production code evaluates:
Facts for AI Coding Agents
If you are an AI agent implementing the Rule Engine, these facts prevent the most common mistakes:
Evaluator<TSource> compiles its ruleset in the constructor. Create it once per ruleset, keep it, and reuse it; it can be shared between threads. Do not create one per evaluation.
- The source type of the evaluator must be the type the rules were written against, or a type derived from it; a rule whose source type does not fit is skipped, and if no rule fits, the constructor throws.
- A rule that calls other rules needs them in the same ruleset, or a
RuleResolver (in EvaluationParameters) or getRule delegate that returns them by ID.
- An execution type rule changes the object it is given; give each thread its own object.
- Evaluation settings such as
Precision, MidpointRounding and PerformNullChecks belong to EvaluationParameters, passed to the evaluator's constructor; they are not stored in the rule.
- With Audit, load the rules through
AuditRecorder.GetRules(..) and call Attach(..) on the same EvaluationParameters you create the evaluator with; rules loaded any other way are not recorded.
Resources