-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
97 lines (73 loc) · 3.24 KB
/
Copy pathProgram.cs
File metadata and controls
97 lines (73 loc) · 3.24 KB
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
using DryIoc;
using SquidStd.Abstractions.Data.Internal.Commands;
using SquidStd.Abstractions.Extensions.Commands;
using SquidStd.Core.Interfaces.Commands;
using SquidStd.Services.Core.Extensions;
#region step-1
// Register the dispatcher and the handlers. EchoCommand has two handlers — both run on dispatch.
var container = new Container();
container.RegisterCommandDispatcher<Session>();
container.RegisterCommandHandler<PingCommand, Session, PingHandler>();
container.RegisterCommandHandler<EchoCommand, Session, EchoHandler>();
container.RegisterCommandHandler<EchoCommand, Session, AuditHandler>();
var dispatcher = container.Resolve<ICommandDispatcher<Session>>();
// At runtime SquidStdBootstrap starts CommandDispatcherActivator<Session>, which performs this
// auto-subscription. Done inline here to keep the sample self-contained.
foreach (var registration in container.Resolve<List<CommandHandlerRegistration<Session>>>())
{
registration.Subscribe(dispatcher, container);
}
#endregion
#region step-2
// The context (here a Session) is passed explicitly at dispatch time — in a server this is the
// session the message arrived on. See RegisterSeededCommandDispatcher for building it from a seed.
var session = new Session();
await Dispatch(dispatcher, new PingCommand(), session);
await Dispatch(dispatcher, new EchoCommand("hello world"), session);
#endregion
#region step-3
// The result reports whether any handler matched and how many ran — here nothing is registered
// for UnknownCommand, so Matched is false and HandlerCount is 0.
var unknown = await dispatcher.DispatchAsync(new UnknownCommand(), session);
Console.WriteLine($"UnknownCommand -> matched={unknown.Matched} handlers={unknown.HandlerCount}");
#endregion
return;
static async Task Dispatch<TCommand>(ICommandDispatcher<Session> dispatcher, TCommand command, Session context)
where TCommand : ICommand
{
var result = await dispatcher.DispatchAsync(command, context);
Console.WriteLine(
$"{typeof(TCommand).Name} -> matched={result.Matched} handlers={result.HandlerCount} errors={result.Errors.Count}"
);
}
internal sealed class Session
{
public string Id { get; } = Guid.NewGuid().ToString("N")[..8];
}
internal sealed record PingCommand : ICommand;
internal sealed record EchoCommand(string Text) : ICommand;
internal sealed record UnknownCommand : ICommand;
internal sealed class PingHandler : ICommandHandler<PingCommand, Session>
{
public Task HandleAsync(PingCommand command, Session context, CancellationToken cancellationToken = default)
{
Console.WriteLine($"[{context.Id}] pong");
return Task.CompletedTask;
}
}
internal sealed class EchoHandler : ICommandHandler<EchoCommand, Session>
{
public Task HandleAsync(EchoCommand command, Session context, CancellationToken cancellationToken = default)
{
Console.WriteLine($"[{context.Id}] echo: {command.Text}");
return Task.CompletedTask;
}
}
internal sealed class AuditHandler : ICommandHandler<EchoCommand, Session>
{
public Task HandleAsync(EchoCommand command, Session context, CancellationToken cancellationToken = default)
{
Console.WriteLine($"[{context.Id}] audit: echo len={command.Text.Length}");
return Task.CompletedTask;
}
}