📎 Webclip
There and Back Again
The post compares the same bank-account withdrawal app in C# and F#. In the C# version, Entity Framework tracking keeps the modified object alive in the context, so the displayed balance changes even when the user does not confirm saving. The fix is to detach the object with AsNoTracking() and attach it again only when the update is confirmed.
In the F# version, the domain model is an immutable record and withdraw returns a copy with the new balance. Data access is kept separate from the domain logic, and the console app confirms the withdrawal before persisting the updated record. The conclusion is that immutability makes the separation between reading, changing, and saving data clearer, while good OOP code requires more discipline.
Reading notes#
- The author spent six months in Indianapolis and took part in conferences and meetups there.
- Functional programming drew attention for three reasons: unfamiliarity, its potential, and Dave Fancher’s enthusiasm.
- The example application reads a bank account, withdraws an amount, asks for confirmation, saves the balance only if approved, and repeats.
- In the first C# version,
BankAccountis a mutable entity with aWithdrawmethod that changesBalance. - The first C# implementation uses Entity Framework context tracking, so the in-memory object changes even before the save.
- If the user refuses to confirm, asking for the same account again can still show the modified balance.
- The revised C# version loads the account with
AsNoTracking()and marks it modified only after confirmation. - In F#,
BankAccountis a record andwithdrawcreates a new record with the updated balance. - The F# data access layer maps the record to SQL Server and keeps the application code detached from the database.
- The F# console app mirrors the C# workflow, but the author says it was harder only because they are more used to OOP.
- The conclusion says immutability helps define the boundary between data access and data processing.
- The author says good OOP code needs discipline, while the functional style is more formal and more intuitive only to a point.
