-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathValidationExample.cs
More file actions
96 lines (83 loc) · 2.78 KB
/
Copy pathValidationExample.cs
File metadata and controls
96 lines (83 loc) · 2.78 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
using Codefinity.EventEmitter.Sample.Infrastructure;
using Codefinity.EventEmitter.Sample.Orders;
using Microsoft.Extensions.DependencyInjection;
namespace Codefinity.EventEmitter.Sample.Examples;
/// <summary>
/// Listener ids (default and explicit), and the errors registration raises for invalid listener methods.
/// </summary>
internal static class ValidationExample
{
private sealed class IdsListener
{
[ApplicationModuleListener]
public void DefaultId(OrderCompleted evt) { }
[ApplicationModuleListener(Id = "loyalty.award-points")]
public void ExplicitId(OrderCompleted evt) { }
}
private sealed class NoParameters
{
[ApplicationModuleListener]
public void On() { }
}
private sealed class ExtraParameter
{
[ApplicationModuleListener]
public void On(OrderCompleted evt, string note) { }
}
private sealed class StaticMethod
{
[ApplicationModuleListener]
public static void On(OrderCompleted evt) { }
}
private sealed class ReturnsInt
{
[ApplicationModuleListener]
public int On(OrderCompleted evt) => 0;
}
private sealed class NoListenerMethods
{
public void On(OrderCompleted evt) { }
}
private sealed class SameIdAgain
{
[ApplicationModuleListener(Id = "loyalty.award-points")]
public void On(OrderCompleted evt) { }
}
public static async Task RunAsync()
{
await using var app = await ExampleApp.StartAsync(services => services
.AddEventEmitter()
.AddListener<IdsListener>());
app.Say("Listener ids stored with each publication:");
await app.PublishAsync(new OrderCompleted("order-1", "alice"));
await app.WaitForCompletedAsync(2);
foreach (var p in await app.Get<ICompletedEventPublications>().FindAllAsync())
{
app.Say($" {p.ListenerId}");
}
app.Say("Registering invalid listeners fails immediately, naming the method:");
foreach (var invalid in new[]
{
typeof(NoParameters), typeof(ExtraParameter), typeof(StaticMethod),
typeof(ReturnsInt), typeof(NoListenerMethods),
})
{
try
{
new ServiceCollection().AddEventEmitter().AddListener(invalid);
}
catch (InvalidOperationException ex)
{
app.Say($" {invalid.Name}: {ex.Message}");
}
}
try
{
new ServiceCollection().AddEventEmitter().AddListener<IdsListener>().AddListener<SameIdAgain>();
}
catch (InvalidOperationException ex)
{
app.Say($" {nameof(SameIdAgain)}: {ex.Message}");
}
}
}