-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathStateMachineDemo.cs
80 lines (67 loc) · 2.73 KB
/
StateMachineDemo.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
using System;
using System.Data.Common;
using System.Threading.Tasks;
using Demo.SmartCache.GrainImplementations;
using Demo.SmartCache.GrainInterfaces;
using Demo.SmartCache.GrainInterfaces.State;
using Orleans;
using Patterns.DevBreadboard;
using Patterns.StateMachine.Implementation;
using Patterns.StateMachine.Interface;
namespace Patterns.SmartCache.Host
{
internal class StateMachineDemo
{
public static async Task Run()
{
var bankAccountStateMachine =
GrainClient.GrainFactory.GetGrain<IBankAccountStateMachineGrain>(Constants.SingleBankAccountGrainId);
var data = await bankAccountStateMachine.GetBalance();
PrintBalance(data.Balance);
data = await bankAccountStateMachine.Deposit(new BankAccountStateMachineAmount(100.0M));
PrintBalance(data.Balance);
data = await bankAccountStateMachine.Withdraw(new BankAccountStateMachineAmount(20.0M));
PrintBalance(data.Balance);
try
{
data = await bankAccountStateMachine.Close();
PrintBalance(data.Balance);
}
catch (InvalidMessage)
{
Console.WriteLine("Tried to close an account which wasn't in ZeroBalance State and failed as expected!");
}
data = await bankAccountStateMachine.Withdraw(new BankAccountStateMachineAmount(100.0M));
PrintBalance(data.Balance);
try
{
data = await bankAccountStateMachine.Withdraw(new BankAccountStateMachineAmount(10.0M));
PrintBalance(data.Balance);
}
catch (InvalidMessage)
{
Console.WriteLine("Tried to withdraw from an Overdrawn account and failed as expected!");
}
data = await bankAccountStateMachine.Deposit(new BankAccountStateMachineAmount(20.0M));
PrintBalance(data.Balance);
data = await bankAccountStateMachine.Close();
PrintBalance(data.Balance);
try
{
data = await bankAccountStateMachine.Deposit(new BankAccountStateMachineAmount(10.0M));
PrintBalance(data.Balance);
}
catch (InvalidMessage)
{
Console.WriteLine("Tried to deposit into a Closed account and failed as expected!");
}
}
private static void PrintBalance(BankAccountStateMachineBalance balance)
{
Console.WriteLine(balance.Match(() => "Zero Balance",
_ => $"Active Balance: {_.Value}",
_ => $"Overdrawn Balance: {_.Value}"));
DevelopmentSiloHost.WaitForInteraction();
}
}
}