C#用字符串表达式执行引擎消除掉if else if

背景

最近我搞了个微信机器人,@机器人 xxx 这样来发送命令

能拿到的信息有,消息内容,消息发送人,消息所在的群id等

需要根据消息内容或者消息发送群id等不同的条件组合来决定走哪个处理逻辑。.

简单来说的话,就用很多if else if

if(model.context.StartsWith("命令1") && model.from == "群1"){
   // 处理命令1 对应的逻辑 
}else if(xxxx){
   // 处理命令2 对应的逻辑 
}else if(yyyy){
   // 处理命令3 对应的逻辑
}

C#用字符串表达式执行引擎消除掉if else if

// 继承BaseRobotAction抽象类实现Do方法
[RobotAction("Context.StartsWith(\"Command1\") AND From == \"123\"")]
public class Command1 : BaseRobotAction
{
    public override Task Do(HttpContext context, VxRobotVm model)
    {
        Console.WriteLine($"{model.From} : {model.Context}");
        return Task.CompletedTask;
    }
}

// 继承BaseRobotAction抽象类实现Do方法
[RobotAction("Context.StartsWith(\"Command2\") OR From == \"234\"")]
public class Command2 : BaseRobotAction
{
    public override Task Do(HttpContext context, VxRobotVm model)
    {
        Console.WriteLine($"{model.From} : {model.Context}");
        return Task.CompletedTask;
    }
}

C#用字符串表达式执行引擎消除掉if else if

/// <summary>
/// 自定义注解
/// </summary>
[Component]
public class RobotAction : Attribute
{

    /// <summary>
    /// 默认注册到容器为IRobotAction类型
    /// </summary>
    [AliasFor(typeof(Component), "Services")]
    public Type[] Services { get; set; } = new[] { typeof(IRobotAction) };
    
    public RobotAction(string expression)
    {
        Expression = expression;
    }

    /// <summary>
    /// 容器中拿此类的时候执行的方法
    /// </summary>
    [AliasFor(typeof(Component), "InitMethod")]

    public string InitMethod { get; set; } = nameof(BaseRobotAction.Init);
    
    /// <summary>
    /// 表达式
    /// </summary>
    public string Expression { get; set; }
}

C#用字符串表达式执行引擎消除掉if else if

// 读取post body
var robotMsg = await ReadBodyAsync<VxRobotVm>(context.Request.Body);
// 从容器中拿到表达式引擎
var engine = context.RequestServices.GetAutofacRoot().Resolve<ExpressionEngine>();
// 从容器中拿到注册为robotAction的所有实例
var actions = context.RequestServices.GetAutofacRoot().Resolve<IEnumerable<IRobotAction>>();
foreach (var action in actions)
{
    // 由于配置了InitMethod方法,容器中获取的时候会触发走InitMethod方法,拿到当前的实上打的RobotAction注解
    var robotActionAttr = action.getRobotActionAttr();
    if (!engine.Execute(robotActionAttr.Expression, robotMsg).IsSuccess) continue;
    // 找到满足条件的action
    await action.DoAction(context,robotMsg);
    break;
}

代码总体不超过200行,详细请移步

Demo

https://github.com/yuzd/FastExpressionEngine/tree/master/Demo

字符串表达式执行引擎 NUGET

开源地址

https://github.com/yuzd/FastExpressionEngine

Install-Package FastExpressionEngine

Document

var bre = new ExpressionEngine();
dynamic datas = new ExpandoObject();
datas.count = 1;
datas.name = "avqqq";
var inputs = new dynamic[]
{
 datas
};

var resultList =
bre.Execute("count < 3 AND name.Contains(\"av\") AND name.StartsWith(\"av\")", inputs);

var resultListIsSuccess = resultList.IsSuccess;
  • 项目参考
https://github.com/microsoft/RulesEngine
  • 表达式编译采用
https://github.com/dadhi/FastExpressionCompiler
  • 缓存采用LRU默认1000size
https://github.com/bitfaster/BitFaster.Caching