This commit is contained in:
Oli Sturm
2026-04-21 14:43:06 +01:00
commit 3e9f0b56c9
11 changed files with 258 additions and 0 deletions
+35
View File
@@ -0,0 +1,35 @@
namespace CsharpOop.Domain;
/// Domain entity / aggregate root representing a bank account
public sealed class Account : AggregateRoot<AccountId>
{
public Money Balance { get; private set; }
// Often present to satisfy serializers / ORMs.
private Account()
{
Balance = new Money(0);
}
public Account(AccountId 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.");
if (Balance.Amount - amount.Amount < 0)
throw new InvalidOperationException("Balance cannot go below zero.");
Balance = Balance.Subtract(amount);
}
}