- 新建类 作为指令声明类( 推荐命名
XxxCommand) - 引用
CherryAya_CommandRoute.Entities - 实现
ICommand接口 - 属性
Name赋值为 指令名 ( string ) - 属性
Description赋值为 指令描述 ( string? ) - 属性
Structure赋值为 根指令的 指令结构实例 ( ICommandStructure )
指令结构详见下条
- 新建类 作为指令结构声明类( 推荐命名
XxxCommandStructure)
或
指令声明类 同级建立新类 作为指令结构声明类 - 引用
CherryAya_CommandRoute.Entities - 实现
ICommandStructure接口 - 属性
Key赋值为 指令键 ( string ) - 如有子指令 属性
Options赋值为 子指令的 指令结构实例 ( List<ICommandStructure>? )
反之 赋值为null - 如该级指令键有值 属性
hasValue赋值为true( bool )
反之 赋值为false - 属性
Value赋值为null( object? ) - 在方法
Handle中处理该级指令触发的业务逻辑
指令路由默认使用配置为
.Entities.impl.defaultRouteConfiguration ( internal )
本节介绍自定义指令路由配置
- 新建类 作为自定义指令路由配置类 ( 推荐命名
RouteConfiguration) - 实现
IRouteConfiguration接口 - 属性
CommandPrefix赋值为 指令匹配前缀 ( string[] )
注意: 每个指令匹配前缀都不应超过一个字符 - 属性
CommandSplit赋值为 消息段分割符号( string )
推荐符号为空格 不推荐超过一个字符 - 如需指令匹配严格大小写 属性
IsCaseSensitive赋值为true( bool )
反之 赋值为false - 在 指令路由 的 有参构造方法 使用 本自定义配置 详见下条
引用 CherryAya_CommandRoute
- 无参构造方法
CommandRoute route = new(); - 有参构造方法 根据上节有自定义指令路由配置类
RouteConfiguration
CommandRoute route = new(new RouteConfiguration());
根据上节 有指令路由实例 route 和指令声明 XxxCommand
使用指令组方法 Register 注册指令
route.Register(new XxxCommand());
设已注册指令 /ping 并有消息 /ping 被业务逻辑存入变量 message( string )
使用路由方法 Execute 执行分发
route.Execute(message)
返回结果为布尔值
true 为匹配成功并执行Handle方法
false 为匹配失败
引用
using CherryAya_CommandRoute.Entities;指令声明
public class TestCommand : ICommand
{
public string Name { get; set; } = "Test";
public string? Description { get; set; } = null;
public ICommandStructure Structure { get; set; } = new TestCommandStructure();
}指令结构
public class TestCommandStructure : ICommandStructure
{
public string Key { get; set; } = "test";
public bool hasValue { get; set; } = true;
public object? Value { get; set; } = null;
public List<ICommandStructure>? Options { get; set; } = new List<ICommandStructure>()
{
new OptionA()
};
public void Handle()
{
Console.WriteLine(Value.ToString());
}
}
public class OptionA : ICommandStructure
{
public string Key { get; set; } = "optionA";
public bool hasValue { get; set; } = false;
public object? Value { get; set; } = null;
public List<ICommandStructure>? Options { get; set; } = null;
public void Handle()
{
Console.WriteLine("location: Test>OptionA");
}
}注册分发
using CherryAya_CommandRoute;
CommandRoute route = new();
route.Register(new TestCommand());
route.Execute("/test hello"); // 执行到 TestCommandStructure.Handle()
route.Execute("/test optionA"); // 执行到 TestCommandStructure.Options[0].Handle()