-
Notifications
You must be signed in to change notification settings - Fork 0
/
AlternativeScopeExample.cs
73 lines (62 loc) · 2.78 KB
/
AlternativeScopeExample.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
using ECF;
using ECF.BaseKitCommands;
using ECF.Engine;
using Microsoft.Extensions.DependencyInjection;
using System.Reflection;
namespace Example;
// We can use diffrent attribute to seperate commands inside same assembly
public class DirectionCommandAttribute : Attribute, ICommandAttribute
{
public string[]? Aliases { get; set; }
public string Name { get; set; }
public DirectionCommandAttribute(string name)
{
Name = name;
}
}
// This command will switch to new scope. It's need to be visible inside previous scope.
[Command("directions")]
public class SwitchToDirectionContextCommand : CommandBase
{
private readonly InterfaceContext interfaceContext;
public SwitchToDirectionContextCommand(InterfaceContext interfaceContext)
{
this.interfaceContext = interfaceContext;
}
public override void Execute()
{
interfaceContext.Prefix = "$"; // we can also change prefix
interfaceContext.CommandProcessor = CreateDirectionCommandsProcessor(interfaceContext);
interfaceContext.DefaultCommand = typeof(HelpCommand); // we can also set default command (keep in mind that command need to be able to be resolved by IoC)
}
public ICommandProcessor CreateDirectionCommandsProcessor(InterfaceContext interfaceContext)
{
// CommandProcessors hold IoC containers to maintain seperate collection of services
return new ServiceCollection()
// when using alternative scope, we need to build command registry manually
.AddECFCommandRegistry(interfaceContext, builder => builder
.RegisterCommands<DirectionCommandAttribute>(Assembly.GetExecutingAssembly()) // we can register all commands with specified attribute in specified assembly
.RegisterCommands<CommandAttribute>(typeof(HelpCommand).Assembly) // this line will register basic commands as HelpCommand, LoadCommand etc.
.Register<CommandAttribute>(typeof(ExitCommand)) // alternatively you can always register commands seperatly one by one
.Register(typeof(ExitCommand), "exit")) // or even register without attribute (it can cause issues with help command)
.BuildAndCreateECFCommandProcessor(); // at the end we need to construct CommandProcesor which will process command requests
}
}
[DirectionCommand("left")]
public class LeftCommand : ICommand
{
public Task ExecuteAsync(CommandArguments args, CancellationToken cancellationToken)
{
Console.WriteLine("<<<<<");
return Task.CompletedTask;
}
}
[DirectionCommand("right")]
public class RightCommand : ICommand
{
public Task ExecuteAsync(CommandArguments args, CancellationToken cancellationToken)
{
Console.WriteLine(">>>>>>");
return Task.CompletedTask;
}
}