add simplification steps

This commit is contained in:
Oli Sturm
2026-04-21 23:04:56 +01:00
parent 62ced3935c
commit b193f64861
20 changed files with 392 additions and 4 deletions
+37
View File
@@ -0,0 +1,37 @@
namespace CsharpFp2.Domain;
/// Domain entity / aggregate root representing a bank account
public sealed class Account
{
public Guid Id { get; private set; }
public Money Balance { get; private set; }
// Often present to satisfy serializers / ORMs.
private Account()
{
Balance = new Money(0);
}
public Account(Guid id, Money openingBalance)
{
if (openingBalance.Amount < 0)
throw new ArgumentOutOfRangeException(nameof(openingBalance));
Id = id;
Balance = openingBalance;
}
// Domain behaviour attached to the entity.
public void Withdraw(Money amount)
{
if (amount.Amount <= 0)
throw new InvalidOperationException("Withdrawal amount must be positive.");
// Sometimes, validation is modelled with custom exceptions instead
if (Balance.Amount - amount.Amount < 0)
throw new InsufficientBalanceException(Balance, amount);
Balance = Balance.Subtract(amount);
}
}