RulesEngine is a library/NuGet package used to abstract business logic/rules/policies from the system. This can work in a very simple way by enabling you to place rules in storage outside of the core logic of the system, ensuring that any changes to the rules do not affect the core system.
Applicable scenarios:
- The number of input and output types is relatively fixed, but the execution logic changes frequently.
- switch conditions change frequently, and complex switch statements are replaced;
- business logic with multiple conditions or rules that will change;
- Scenarios where the rule freedom does not require particularly high degrees of freedom. (In this case, a scripting engine is recommended)
The rules of RulesEngine are stored in JSON, and the rules (Rules) are expressed in lambda expressions.
GitHub address:The hyperlink login is visible.
Document address:The hyperlink login is visible.(I personally feel that the documentation is a bit vague, and some usage is not explained too clearly)
First, create a new .NET Core 3.1 console project and use the nuget command to install the package, as follows:
Create a new rule file :Rules.json set to copy to the output directory.
The configuration is as follows:
Try running the program and the output is as follows:
The customTest workflow will call our C# function to validate the rules, note that when instantiating the RulesEngine object, we need to add ReSettings to execute our C# method normally.
Among them,Validation sets are more abstract to understand, will use localParams, the keyword of the local variable is localParams, which can be simply understood as var name = expression
The full code is as follows:
Source code download:
Tourists, if you want to see the hidden content of this post, please Reply
|