Architecture
RulesEngine is a library that you embed in your application. It does not own your rules or your inputs: your application loads the rules from wherever it keeps them, hands them to the engine, and acts on the results.
System context
The rules can live in any store that can produce a Workflow (JSON files, blob storage, Cosmos DB, SQL through Entity Framework, Azure App Configuration and so on). The part of your application that connects the store, the inputs and the engine is called the wrapper.
flowchart LR
store[("Rules store<br/>JSON files, blob storage, Cosmos DB,<br/>SQL via EF, App Configuration")]
inputs["Input(s)<br/>objects, anonymous types,<br/>ExpandoObject, dictionaries"]
subgraph app["Your application (the wrapper)"]
load["Load rules and build<br/>Workflow objects"]
run["Call ExecuteAllRulesAsync<br/>with a workflow name and inputs"]
act["Act on the results"]
end
engine["RulesEngine library"]
store -->|"workflow JSON or rows"| load
inputs --> run
load -->|"constructor / AddWorkflow"| engine
run -->|"workflow name + inputs"| engine
engine -->|"list of RuleResultTree"| act
Figure 1. RulesEngine in context: rules and inputs come from your application, results go back to it.
Components
flowchart TB
subgraph pub["Public API"]
RE["RulesEngine<br/>(implements IRulesEngine)"]
RS["ReSettings"]
RP["RuleParameter"]
RT["RuleResultTree"]
AB["ActionBase and ActionContext<br/>(custom actions)"]
PX["RuleExpressionParser<br/>(also usable standalone)"]
end
subgraph internal["Internal pipeline"]
VAL["Validators<br/>(FluentValidation)"]
CACHE["RulesCache<br/>workflows, compiled rules, globals"]
COMP["RuleCompiler"]
FACT["RuleExpressionBuilderFactory"]
LEB["LambdaExpressionBuilder"]
HELP["Helpers<br/>result tree and error policy"]
AF["ActionFactory<br/>OutputExpression, EvaluateRule, custom"]
end
subgraph deps["Libraries"]
DLC["System.Linq.Dynamic.Core<br/>parses expression strings"]
FEC["FastExpressionCompiler<br/>optional compiler"]
CTP["CustomTypeProvider<br/>type allow-list"]
end
RE --> VAL
RE --> CACHE
RE --> COMP
RE --> AF
COMP --> FACT --> LEB
LEB --> PX
LEB --> HELP
PX --> DLC
PX -.-> FEC
PX --> CTP
RS -. configures .-> RE
RP -. input of .-> RE
RE -. returns .-> RT
AB -. extends .-> AF
Figure 2. The main building blocks. Solid arrows show who calls whom; dotted arrows show configuration and data.
| Component | Visibility | Responsibility |
|---|---|---|
RulesEngine |
public | Facade that implements IRulesEngine: workflow management, compile-on-demand, rule execution, error-message formatting and action orchestration. |
ReSettings |
public | Behavior switches such as error policy, case sensitivity, caching and compiler options. |
| Workflow validators | internal | FluentValidation rules that run in AddWorkflow and AddOrUpdateWorkflow and throw RuleValidationException. |
RulesCache |
internal | Stores workflows, compiled rules and compiled global-parameter delegates, and resolves WorkflowsToInject. |
RuleCompiler |
internal | Turns a Rule into a delegate: leaf expressions, nested And/Or rules, and local parameters. |
RuleExpressionBuilderFactory, LambdaExpressionBuilder |
internal | Select the builder for a RuleExpressionType (only LambdaExpression exists today) and convert parse errors into failed-rule results. |
RuleExpressionParser |
public | Parses expression strings with System.Linq.Dynamic.Core, compiles them and caches the delegates. Can be used on its own as an expression evaluator. |
CustomTypeProvider |
public | Decides which .NET types expressions may call into. See the type security model. |
ActionFactory and actions |
internal / public | Creates the built-in OutputExpression and EvaluateRule actions and any custom actions registered in ReSettings.CustomActions. |
Helpers |
internal | Builds RuleResultTree objects and applies the exception policy. |
Object model
Workflows are plain data. The JSON you store maps one to one onto these classes (the JSON schema is in schema/workflow-schema.json).
classDiagram
class Workflow {
+string WorkflowName
+IEnumerable~string~ WorkflowsToInject
+RuleExpressionType RuleExpressionType
+IEnumerable~ScopedParam~ GlobalParams
+IEnumerable~Rule~ Rules
}
class Rule {
+string RuleName
+bool Enabled
+string Expression
+string Operator
+IEnumerable~Rule~ Rules
+IEnumerable~ScopedParam~ LocalParams
+string ErrorMessage
+string SuccessEvent
+RuleActions Actions
}
class ScopedParam {
+string Name
+string Expression
}
class RuleActions {
+ActionInfo OnSuccess
+ActionInfo OnFailure
}
class ActionInfo {
+string Name
+Dictionary Context
}
class RuleResultTree {
+Rule Rule
+bool IsSuccess
+IEnumerable~RuleResultTree~ ChildResults
+Dictionary Inputs
+ActionResult ActionResult
+string ExceptionMessage
}
class ActionResult {
+object Output
+Exception Exception
}
class ActionRuleResult {
+List~RuleResultTree~ Results
}
Workflow "1" o-- "*" Rule : Rules
Workflow "1" o-- "*" ScopedParam : GlobalParams
Rule "1" o-- "*" Rule : child rules
Rule "1" o-- "*" ScopedParam : LocalParams
Rule "1" o-- "0..1" RuleActions : Actions
RuleActions "1" o-- "0..2" ActionInfo
RuleResultTree "1" --> "1" Rule
RuleResultTree "1" o-- "*" RuleResultTree : ChildResults
RuleResultTree "1" --> "0..1" ActionResult
ActionRuleResult --|> ActionResult
Figure 3. The workflow model (input) and the result model (output). Only the most important members are shown.
Libraries RulesEngine builds on
| Library | Used for |
|---|---|
| System.Linq.Dynamic.Core | Parsing expression strings into expression trees and providing the expression language. |
| FastExpressionCompiler | Optional faster compilation of expression trees (ReSettings.UseFastExpressionCompiler). |
| FluentValidation | Validating workflows when they are added. |
| System.Text.Json | Deserializing workflow JSON, action contexts and formatting error messages. |
Where to go next
- Evaluation pipeline: what happens during
ExecuteAllRulesAsync. - Caching and invalidation: what is cached, and when it is refreshed.
- How inputs are typed: how
ExpandoObject, dictionaries and POCOs become typed parameters.