In the previous article, we introduced the points system and its core rules engine. Here we try to implement one of the common points acquisition events mentioned in the previous article using an open source rules engine.

The RulesEngine we use here is the RulesEngine of Microsoft’s open source.net platform. It uses the JSON format and describes the rules through lambda expressions.

Introduction of depend on

You can also use the NuGet Package Manager or the command line tool such as dotnet to install the RulesEngine. The specific commands can be found on the NuGet website

Writes regular data in JSON format

Here’s one of the simplest rules, using registering for credits as an example:

[{"WorkflowName": "Register"."Rules": [{"RuleName": "Give100Points"."SuccessEvent": "100"."RuleExpressionType": "LambdaExpression"."Expression": "true"}}]]Copy the code

Note that C# uses big camel by default, which is a little different from JSON’s camel.

Parameter names type instructions
array Top-level elements, with a single element of type WorkflowRules

WorkflowRules Parameter description

Parameter names type instructions
WorkflowName string Execute specifies the rule to be executed based on this parameter
Rules array A single element is a Rule. Rules can be connected by And, AndAlso, Or, OrElse. A single element is of type Rule

Rule Parameter Description

Parameter names type instructions
RuleName string Rule name
SuccessEvent string Is passed as a parameter in the onSuccess event
RuleExpressionType string Now it can only be “LambdaExpression”
Expression string For lambda expressions that are judged according to the rules based on the input parameters, no parameters are passed in the registration process, and simply return true

Use rules

public class RuleService
{
    private readonly RulesEngine.RulesEngine _engine;

    public RuleService()
    {
        const string data = @" [ { ""WorkflowName"": ""Register"", ""Rules"": [ { ""RuleName"": ""Give100Points"", ""SuccessEvent"": ""100"", ""RuleExpressionType"": ""LambdaExpression"", ""Expression"": ""true"" } ] } ]";

        var workflowRules = JsonConvert.DeserializeObject<WorkflowRules[]>(data);
        _engine = new RulesEngine.RulesEngine(workflowRules);
    }

    public async Task<string> Verify()
    {
        string discountOffered = null;
        var resultList = await _engine.ExecuteAllRulesAsync("Register", Array.Empty<object> ()); resultList.OnSuccess(it => discountOffered =$"Register success and get {it} points");
        returndiscountOffered; }}Copy the code

Code instructions

  • Convert our JSON into an array of type WorkflowRules in the constructor and then use it to initialize a _Engine of type RulesEngine. Later optimizations allow the _engine to be implemented externally and then injected by IOC.
  • The ExecuteAllRulesAsync method of _engine is called at use time, and the first parameter is passed to be the same as the WorkflowName in JSON. The second parameter is passed to validate. No parameters were passed at registration time, so an empty array is written here. Note that this method is an asynchronous method and we are firing it here with the await keyword.
  • The OnSuccess function returns a value when it determines success, and the argument it receives is SuccessEvent in JSON.
  • Because there is no failure to register an event, the OnFail method is not registered.
  • The result is returned as a network request encapsulated in the Controller.

Invoke the sample

At this point, a minimalist rule is implemented.