-
Notifications
You must be signed in to change notification settings - Fork 0
/
OrderPlacementCoordinator.cs
158 lines (136 loc) · 4.11 KB
/
OrderPlacementCoordinator.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
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
using System;
public class PurchaseItemCommand : Message
{
public string ItemId { get; set; }
public int Quantity { get; set; }
}
public class PurchaseItemResponse : Message
{
public bool Succeeded { get; set; }
public string ErrorCode { get; set; }
}
public class CreditCommand : Message
{
public string ExternalTransationId { get; set; }
public double Amount { get; set; }
public string Description { get; set; }
}
public class CreditResponse : Message
{
public string ExternalTransationId { get; set; }
public string InternalTransationId { get; set; }
public bool Succeeded { get; set; }
public string ErrorCode { get; set; }
}
public class VoidTransactionCommand : Message
{
public string ExternalTransationId { get; set; }
}
public class VoidTransactionResponse : Message
{
public bool Succeeded { get; set; }
public string ErrorCode { get; set; }
}
public class ReserveItemCommand : Message
{
public string ItemId { get; set; }
public int Quantity { get; set; }
public bool AllOrNothing { get; set; }
}
public class ReserveItemResponse : Message
{
public string ItemId { get; set; }
public int Quantity { get; set; }
public bool Succeeded { get; set; }
public string ErrorCode { get; set; }
}
public class OrderPlacementCoordinator : Actor,
IReceive<PurchaseItemCommand>,
IReceive<CreditResponse>,
IReceive<ReserveItemResponse>,
IReceive<VoidTransactionResponse>
{
private PersistedValue<Guid> transactionId;
private PersistedValue<ActorRef> requestor;
private PersistedValue<string> itemId;
private PersistedValue<int> quantity;
private PersistedValue<string> reservationError;
public void Receive(PurchaseItemCommand command)
{
this.transactionId = PersistedValue.Create(Guid.NewGuid());
this.requestor = PersistedValue.Create(command.Sender);
this.itemId = PersistedValue.Create(command.ItemId);
this.quantity = PersistedValue.Create(command.Quantity);
var paymentActor = this.System.GetActorRef(
type: "Payment",
id: transactionId.Value);
paymentActor.Send(new CreditCommand
{
ExternalTransationId =
transactionId.Value.ToString(),
// Hard-coded for simplicity's sake.
Amount = 99.95,
Description = "Coffee Beans 1lb"
});
}
public void Receive(CreditResponse response)
{
if (response.Succeeded)
{
var warehouseActor = this.System.GetActorRef(
type: "Warehouse",
id: transactionId.Value);
warehouseActor.Send(new ReserveItemCommand
{
ItemId = this.itemId.Value,
Quantity = this.quantity.Value,
AllOrNothing = true
});
}
else
{
this.requestor.Value.Send(
new PurchaseItemResponse
{
Succeeded = false,
ErrorCode = response.ErrorCode
});
this.Terminate();
}
}
public void Receive(ReserveItemResponse response)
{
if (response.Succeeded)
{
this.requestor.Value.Send(
new PurchaseItemResponse
{
Succeeded = true
});
this.Terminate();
}
else
{
this.reservationError =
PersistedValue.Create(response.ErrorCode);
var paymentActor = this.System.GetActorRef(
type: "Payment",
id: transactionId.Value);
paymentActor.Send(new VoidTransactionCommand
{
ExternalTransationId =
transactionId.Value.ToString(),
});
}
}
public void Receive(VoidTransactionResponse response)
{
this.requestor.Value.Send(
new PurchaseItemResponse
{
Succeeded = false,
ErrorCode = this.reservationError.Value
});
this.Terminate();
}
}