add fp1 sample

This commit is contained in:
Oli Sturm
2026-04-22 13:38:09 +01:00
parent 980cc626a9
commit 1b5239566a
8 changed files with 211 additions and 0 deletions
+32
View File
@@ -0,0 +1,32 @@
namespace CsharpFp1.Domain;
public sealed record Account(Guid Id, Money Balance);
public static class AccountDomain
{
// Choosing a "clean" FP approach here of instantiating the Account
// Depending on needs, code to prevent the Account type from
// being instantiated without this helper needs to be added
// to the record type.
public static Account Open(Guid id, Money openingBalance)
{
if (openingBalance.Amount < 0)
throw new ArgumentOutOfRangeException(nameof(openingBalance));
return new Account(id, openingBalance);
}
public static Account Withdraw(Account account, Money amount)
{
if (amount.Amount <= 0)
throw new InvalidOperationException("Withdrawal amount must be positive.");
if (account.Balance.Amount < amount.Amount)
throw new InsufficientBalanceException(account.Balance, amount);
return account with
{
Balance = account.Balance.Subtract(amount),
};
}
}
@@ -0,0 +1,17 @@
namespace CsharpFp1.Domain;
/// Custom domain exception thrown when a withdrawal would cause the balance to go below zero
public sealed class InsufficientBalanceException : InvalidOperationException
{
public Money CurrentBalance { get; }
public Money RequestedAmount { get; }
public InsufficientBalanceException(Money currentBalance, Money requestedAmount)
: base(
$"Insufficient balance. Current: {currentBalance.Amount:0.00}, Requested: {requestedAmount.Amount:0.00}"
)
{
CurrentBalance = currentBalance;
RequestedAmount = requestedAmount;
}
}
+33
View File
@@ -0,0 +1,33 @@
namespace CsharpFp1.Domain;
/// Value object used to represent money and enforce simple invariants.
/// Note that this implementation uses immutable patterns for the data
/// by returning a new instance for each modification. This is an early
/// recommendation for DDD with OO, but not necessarily the common practice
/// in many real-world implementations.
public sealed class Money
{
// Potentially with a setter - see note above
public decimal Amount { get; }
public Money(decimal amount)
{
Amount = amount;
}
// In many existing DDD/OO codebases you may actually see the use
// of mutable value types.
//
// public void Add(Money other)
// {
// this.Amount += other.Amount;
// }
// On the other hand, sometimes these helpers may be left out
// and operations encoded directly "from the outside":
// newBalance = new Money(oldBalance.Amount - charge.Amount)
//
public Money Add(Money other) => new(Amount + other.Amount);
public Money Subtract(Money other) => new(Amount - other.Amount);
}