Skip to content

Commit

Permalink
Change Balance state object to an immutable record
Browse files Browse the repository at this point in the history
  • Loading branch information
egil committed Jan 18, 2025
1 parent 60776fa commit bca5d6b
Showing 1 changed file with 5 additions and 6 deletions.
11 changes: 5 additions & 6 deletions orleans/BankAccount/AccountTransfer.Grains/AccountGrain.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
using AccountTransfer.Interfaces;
using AccountTransfer.Interfaces;
using Orleans.Concurrency;
using Orleans.Transactions.Abstractions;

namespace AccountTransfer.Grains;

[GenerateSerializer]
[GenerateSerializer, Immutable]
public record class Balance
{
[Id(0)]
public int Value { get; set; } = 1_000;
public int Value { get; init; } = 1_000;
}

[Reentrant]
Expand All @@ -22,7 +21,7 @@ public AccountGrain(

public Task Deposit(int amount) =>
_balance.PerformUpdate(
balance => balance.Value += amount);
balance => balance with { Value = balance.Value + amount });

public Task Withdraw(int amount) =>
_balance.PerformUpdate(balance =>
Expand All @@ -35,7 +34,7 @@ public Task Withdraw(int amount) =>
$" This account has {balance.Value} credits.");
}

balance.Value -= amount;
return balance with { Value = balance.Value + amount };
});

public Task<int> GetBalance() =>
Expand Down

0 comments on commit bca5d6b

Please sign in to comment.