From aa19cbe9f5fa8ff66c0c33fa6ffaafacae385ab7 Mon Sep 17 00:00:00 2001 From: Rossimac Date: Fri, 21 Feb 2025 15:20:35 +0000 Subject: [PATCH 1/2] Bug fixes for Orleans Bank Account sample. Balance now updates after Deposit or Withdraw operations. Withdraw decrements now instead of incorrectly incrementing. --- .../BankAccount/AccountTransfer.Grains/AccountGrain.cs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/orleans/BankAccount/AccountTransfer.Grains/AccountGrain.cs b/orleans/BankAccount/AccountTransfer.Grains/AccountGrain.cs index 97b71f422d6..3f6e565ce6e 100644 --- a/orleans/BankAccount/AccountTransfer.Grains/AccountGrain.cs +++ b/orleans/BankAccount/AccountTransfer.Grains/AccountGrain.cs @@ -4,10 +4,10 @@ namespace AccountTransfer.Grains; -[GenerateSerializer, Immutable] -public record class Balance +[GenerateSerializer] +public class class Balance { - public int Value { get; init; } = 1_000; + public int Value { get; set; } = 1_000; } [Reentrant] @@ -21,7 +21,7 @@ public AccountGrain( public Task Deposit(int amount) => _balance.PerformUpdate( - balance => balance with { Value = balance.Value + amount }); + balance => balance.Value += amount); public Task Withdraw(int amount) => _balance.PerformUpdate(balance => @@ -34,7 +34,7 @@ public Task Withdraw(int amount) => $" This account has {balance.Value} credits."); } - return balance with { Value = balance.Value + amount }; + balance = balance.Value -= amount; }); public Task GetBalance() => From 7a91f9899ac73bd0eb69464892221da6d510e7c3 Mon Sep 17 00:00:00 2001 From: Ross McCracken Date: Sun, 8 Jun 2025 20:01:49 +0100 Subject: [PATCH 2/2] 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. --- orleans/BankAccount/AccountTransfer.Grains/AccountGrain.cs | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/orleans/BankAccount/AccountTransfer.Grains/AccountGrain.cs b/orleans/BankAccount/AccountTransfer.Grains/AccountGrain.cs index 3f6e565ce6e..02b37972902 100644 --- a/orleans/BankAccount/AccountTransfer.Grains/AccountGrain.cs +++ b/orleans/BankAccount/AccountTransfer.Grains/AccountGrain.cs @@ -1,16 +1,15 @@ using AccountTransfer.Interfaces; -using Orleans.Concurrency; using Orleans.Transactions.Abstractions; namespace AccountTransfer.Grains; [GenerateSerializer] -public class class Balance +public class Balance { + [Id(0)] public int Value { get; set; } = 1_000; } -[Reentrant] public sealed class AccountGrain : Grain, IAccountGrain { private readonly ITransactionalState _balance; @@ -34,7 +33,7 @@ public Task Withdraw(int amount) => $" This account has {balance.Value} credits."); } - balance = balance.Value -= amount; + balance.Value -= amount; }); public Task GetBalance() =>