Skip to content

Conversation

Rossimac
Copy link
Contributor

Balance now updates after Deposit or Withdraw operations.

Withdraw decrements now instead of incorrectly incrementing.

Balance now updates after Deposit or Withdraw operations.

Withdraw decrements now instead of incorrectly incrementing.
@oising
Copy link

oising commented May 8, 2025

Removing the immutability of the Balance class sets a bad precedent for this sample. The idea of immutability is to prevent the class of bugs where grain A makes a change to a instance, then sends it to grain B -- on the same silo -- and then makes another change to the instance, which, because it's a reference/byref, will change the instance in grain B also.

}

return balance with { Value = balance.Value + amount };
balance = balance.Value -= amount;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The balance = part here is unnecessary - the balance variable goes out of scope immediately after this statement

@ReubenBond
Copy link
Member

@oising I believe this is the correct change, due to how the transactions API works: The delegate passed to PerformUpdate receives a copy of state and it's expected to mutate it.

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.
@Rossimac Rossimac force-pushed the fix-orleans-bank-account-sample branch from 07f28af to 7a91f98 Compare June 8, 2025 19:02
@Rossimac
Copy link
Contributor Author

Rossimac commented Jun 8, 2025

I fixed a couple of build errors in the commit.

Also, I fixed the sample to allow the bank balance to actually persist the balance after a Withdrawal.

@ReubenBond ReubenBond merged commit 4d316c2 into dotnet:main Jun 9, 2025
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants