-
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathSampleRunner.cs
More file actions
131 lines (106 loc) · 4.67 KB
/
Copy pathSampleRunner.cs
File metadata and controls
131 lines (106 loc) · 4.67 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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
using ConsoleSample.Messages;
using Foundatio.Mediator;
namespace ConsoleSample;
public class SampleRunner
{
private readonly IMediator _mediator;
public SampleRunner(IMediator mediator, IServiceProvider serviceProvider)
{
_mediator = mediator;
}
public async Task RunAllSamplesAsync()
{
Console.WriteLine("🚀 Foundatio.Mediator Clean Sample");
Console.WriteLine("===================================\n");
await RunSimpleExamples();
await RunOrderCrudExamples();
await RunCounterStreamExample();
await RunEventPublishingExamples();
Console.WriteLine("\n🎉 All samples completed successfully!");
}
private async Task RunSimpleExamples()
{
Console.WriteLine("1️⃣ Simple Command and Query Examples");
Console.WriteLine("=====================================\n");
// Simple command (no response)
await _mediator.InvokeAsync(new Ping("Hello from mediator!"));
// Simple query (with response)
var greeting = _mediator.Invoke<string>(new GetGreeting("World"));
Console.WriteLine($"✨ {greeting}\n");
}
private async Task RunOrderCrudExamples()
{
Console.WriteLine("2️⃣ Order CRUD with Result Pattern");
Console.WriteLine("==================================\n");
// Create order
Console.WriteLine("📝 Creating order...");
var createResult = await _mediator.InvokeAsync<Result<Order>>(new CreateOrder("CUST-001", 299.99m, "Premium widget"));
if (createResult.IsSuccess)
{
var order = createResult.Value;
Console.WriteLine($"✅ Order created: {order.Id} for ${order.Amount:F2}\n");
// Get order
Console.WriteLine("🔍 Retrieving order...");
var getResult = await _mediator.InvokeAsync<Result<Order>>(new GetOrder(order.Id));
if (getResult.IsSuccess)
{
Console.WriteLine($"✅ Order found: {getResult.Value.Id} - {getResult.Value.Description}\n");
}
// Update order
Console.WriteLine("📝 Updating order...");
var updateResult = await _mediator.InvokeAsync<Result<Order>>(new UpdateOrder(order.Id, 399.99m, "Premium widget - Updated"));
if (updateResult.IsSuccess)
{
Console.WriteLine($"✅ Order updated: ${updateResult.Value.Amount:F2}\n");
}
// Delete order
Console.WriteLine("🗑️ Deleting order...");
var deleteResult = await _mediator.InvokeAsync<Result>(new DeleteOrder(order.Id));
if (deleteResult.IsSuccess)
{
Console.WriteLine($"✅ Order deleted successfully\n");
}
}
else
{
Console.WriteLine($"❌ Failed to create order: {createResult.Message}\n");
}
// Demonstrate validation errors
Console.WriteLine("🚫 Testing validation errors...");
var invalidResult = await _mediator.InvokeAsync<Result<Order>>(new CreateOrder("", -100m, "Invalid order"));
if (!invalidResult.IsSuccess)
{
Console.WriteLine($"❌ Validation failed as expected: {invalidResult.Message}\n");
}
}
private async Task RunCounterStreamExample()
{
Console.WriteLine("3️⃣ Counter Stream Example");
Console.WriteLine("==========================\n");
Console.WriteLine("🔢 Starting counter stream...");
CancellationTokenSource cts = new();
int count = 5;
await foreach (var item in _mediator.Invoke<IAsyncEnumerable<int>>(new CounterStreamRequest(), cts.Token))
{
count--;
if (count == 0)
{
cts.Cancel();
}
Console.WriteLine($"Counter: {item}");
}
Console.WriteLine("✅ Counter stream completed.\n");
}
private async Task RunEventPublishingExamples()
{
Console.WriteLine("4️⃣ Event Publishing (Multiple Handlers)");
Console.WriteLine("========================================\n");
Console.WriteLine("📢 Publishing OrderCreated event (will trigger multiple handlers)...");
await _mediator.PublishAsync(new OrderCreated("ORD-DEMO-001", "CUST-001", 199.99m, DateTime.UtcNow));
Console.WriteLine("\n📢 Publishing OrderUpdated event...");
await _mediator.PublishAsync(new OrderUpdated("ORD-DEMO-001", 249.99m, DateTime.UtcNow));
Console.WriteLine("\n📢 Publishing OrderDeleted event...");
await _mediator.PublishAsync(new OrderDeleted("ORD-DEMO-001", DateTime.UtcNow));
Console.WriteLine();
}
}