-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathProgram.cs
82 lines (77 loc) · 2.73 KB
/
Program.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
74
75
76
77
78
79
80
81
82
using Samples.HelloCart;
using Samples.HelloCart.V1;
using Samples.HelloCart.V2;
using Samples.HelloCart.V3;
using Samples.HelloCart.V4;
using Samples.HelloCart.V5;
using static System.Console;
// Create services
AppBase? app;
var isFirstTry = true;
while(true) {
WriteLine("Select the implementation to use:");
WriteLine(" 1: ConcurrentDictionary-based");
WriteLine(" 2: EF Core + Operations Framework (OF)");
WriteLine(" 3: EF Core + OF + DbEntityResolvers (pipelined fetches)");
WriteLine(" 4: EF Core + OF + DbEntityResolvers + Client-Server");
WriteLine(" 5: EF Core + OF + DbEntityResolvers + Client-Server + Multi-Host");
// WriteLine(" 4: 3 + client-server mode");
Write("Type 1..5: ");
var input = isFirstTry
? args.SingleOrDefault() ?? ReadLine()
: ReadLine();
input = (input ?? "").Trim();
app = input switch {
"1" => new AppV1(),
"2" => new AppV2(),
"3" => new AppV3(),
"4" => new AppV4(),
"5" => new AppV5(),
_ => null,
};
if (app != null)
break;
WriteLine("Invalid selection.");
WriteLine();
isFirstTry = false;
}
await using var appDisposable = app;
await app.InitializeAsync(app.ServerServices);
// Starting watch tasks
WriteLine("Initial state:");
using var cts = new CancellationTokenSource();
_ = app.Watch(app.WatchedServices, cts.Token);
await Task.Delay(700); // Just to make sure watch tasks print whatever they want before our prompt appears
// await AutoRunner.Run(app);
var productService = app.ClientServices.GetRequiredService<IProductService>();
var commander = app.ClientServices.Commander();
WriteLine();
WriteLine("Change product price by typing [productId]=[price], e.g. \"apple=0\".");
WriteLine("See the total of every affected cart changes.");
while (true) {
await Task.Delay(500);
WriteLine();
Write("[productId]=[price]: ");
try {
var input = (ReadLine() ?? "").Trim();
if (input == "")
break;
var parts = input.Split("=");
if (parts.Length != 2)
throw new ApplicationException("Invalid price expression.");
var productId = parts[0].Trim();
var price = decimal.Parse(parts[1].Trim());
var product = await productService.Get(productId);
if (product == null)
throw new KeyNotFoundException("Specified product doesn't exist.");
var command = new EditCommand<Product>(product with { Price = price });
await commander.Call(command);
// You can run absolutely identical action with:
// await app.ClientServices.Commander().Call(command);
}
catch (Exception e) {
WriteLine($"Error: {e.Message}");
}
}
WriteLine("Terminating...");
cts.Cancel();