Skip to content

Commit 4d316c2

Browse files
authored
Bug fixes for Orleans Bank Account sample. (#7019)
* Bug fixes for Orleans Bank Account sample. Balance now updates after Deposit or Withdraw operations. Withdraw decrements now instead of incorrectly incrementing. * Code Review suggestions and bug fixes. Made Balance a class instead of a record as no benefit in using record here. Added Id attribute to Balance.Value so that it is serialized and to allow state to be persisted after a Withdrawal. Removed Reentrant attribute from AccountGrain as this could introduce race conditions when using Transactions.
1 parent b0669e6 commit 4d316c2

1 file changed

Lines changed: 6 additions & 7 deletions

File tree

orleans/BankAccount/AccountTransfer.Grains/AccountGrain.cs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
11
using AccountTransfer.Interfaces;
2-
using Orleans.Concurrency;
32
using Orleans.Transactions.Abstractions;
43

54
namespace AccountTransfer.Grains;
65

7-
[GenerateSerializer, Immutable]
8-
public record class Balance
6+
[GenerateSerializer]
7+
public class Balance
98
{
10-
public int Value { get; init; } = 1_000;
9+
[Id(0)]
10+
public int Value { get; set; } = 1_000;
1111
}
1212

13-
[Reentrant]
1413
public sealed class AccountGrain : Grain, IAccountGrain
1514
{
1615
private readonly ITransactionalState<Balance> _balance;
@@ -21,7 +20,7 @@ public AccountGrain(
2120

2221
public Task Deposit(int amount) =>
2322
_balance.PerformUpdate(
24-
balance => balance with { Value = balance.Value + amount });
23+
balance => balance.Value += amount);
2524

2625
public Task Withdraw(int amount) =>
2726
_balance.PerformUpdate(balance =>
@@ -34,7 +33,7 @@ public Task Withdraw(int amount) =>
3433
$" This account has {balance.Value} credits.");
3534
}
3635

37-
return balance with { Value = balance.Value + amount };
36+
balance.Value -= amount;
3837
});
3938

4039
public Task<int> GetBalance() =>

0 commit comments

Comments
 (0)