Skip to content

Commit 2ea0b65

Browse files
egilIEvangelist
andauthored
Upgrade bank account sample (#7011)
* Upgrade Orleans BankAccount sample to .NET 9 * Change Balance state object to an immutable record * Apply suggestions from code review --------- Co-authored-by: David Pine <[email protected]>
1 parent 8812ba7 commit 2ea0b65

File tree

4 files changed

+14
-15
lines changed

4 files changed

+14
-15
lines changed

orleans/BankAccount/AccountTransfer.Grains/AccountGrain.cs

+5-6
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
1-
using AccountTransfer.Interfaces;
1+
using AccountTransfer.Interfaces;
22
using Orleans.Concurrency;
33
using Orleans.Transactions.Abstractions;
44

55
namespace AccountTransfer.Grains;
66

7-
[GenerateSerializer]
7+
[GenerateSerializer, Immutable]
88
public record class Balance
99
{
10-
[Id(0)]
11-
public int Value { get; set; } = 1_000;
10+
public int Value { get; init; } = 1_000;
1211
}
1312

1413
[Reentrant]
@@ -22,7 +21,7 @@ public AccountGrain(
2221

2322
public Task Deposit(int amount) =>
2423
_balance.PerformUpdate(
25-
balance => balance.Value += amount);
24+
balance => balance with { Value = balance.Value + amount });
2625

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

38-
balance.Value -= amount;
37+
return balance with { Value = balance.Value + amount };
3938
});
4039

4140
public Task<int> GetBalance() =>

orleans/BankAccount/Directory.Build.props

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<Project>
22
<PropertyGroup>
3-
<TargetFramework>net8.0</TargetFramework>
3+
<TargetFramework>net9.0</TargetFramework>
44
<ImplicitUsings>enable</ImplicitUsings>
55
<Nullable>enable</Nullable>
66
</PropertyGroup>

orleans/BankAccount/Directory.Packages.props

+7-7
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@
44
<ManagePackageVersionsCentrally>true</ManagePackageVersionsCentrally>
55
</PropertyGroup>
66
<ItemGroup>
7-
<PackageVersion Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="8.0.0" />
8-
<PackageVersion Include="Microsoft.Extensions.Hosting" Version="8.0.0" />
9-
<PackageVersion Include="Microsoft.Extensions.Logging.Console" Version="8.0.0" />
10-
<PackageVersion Include="Microsoft.Orleans.Client" Version="8.0.0" />
11-
<PackageVersion Include="Microsoft.Orleans.Sdk" Version="8.0.0" />
12-
<PackageVersion Include="Microsoft.Orleans.Server" Version="8.0.0" />
13-
<PackageVersion Include="Microsoft.Orleans.Transactions" Version="8.0.0" />
7+
<PackageVersion Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="9.0.1" />
8+
<PackageVersion Include="Microsoft.Extensions.Hosting" Version="9.0.1" />
9+
<PackageVersion Include="Microsoft.Extensions.Logging.Console" Version="9.0.1" />
10+
<PackageVersion Include="Microsoft.Orleans.Client" Version="9.0.1" />
11+
<PackageVersion Include="Microsoft.Orleans.Sdk" Version="9.0.1" />
12+
<PackageVersion Include="Microsoft.Orleans.Server" Version="9.0.1" />
13+
<PackageVersion Include="Microsoft.Orleans.Transactions" Version="9.0.1" />
1414
</ItemGroup>
1515
</Project>

orleans/BankAccount/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ public Task Withdraw(uint amount) => _balance.PerformUpdate(x =>
8989

9090
## Sample prerequisites
9191

92-
This sample is written in C# and targets .NET 7.0. It requires the [.NET 7.0 SDK](https://dotnet.microsoft.com/download/dotnet/7.0) or later.
92+
This sample is written in C# and targets .NET 9.0. It requires the [.NET 9.0 SDK](https://dotnet.microsoft.com/download/dotnet/9.0) or later.
9393

9494
## Building the sample
9595

0 commit comments

Comments
 (0)