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
@@ -0,0 +1,39 @@
using CsharpOop.Contracts;
using CsharpOop.Domain;
namespace CsharpOop.Applications;
/// Application-layer handler orchestrating the use case
public sealed class WithdrawMoneyHandler
{
private readonly IAccountRepository _repository;
public WithdrawMoneyHandler(IAccountRepository repository)
{
_repository = repository;
}
public void Handle(WithdrawMoneyCommand command)
{
Console.WriteLine(
$"[App] Handling WithdrawMoneyCommand for account {command.AccountId}, amount {command.Amount:0.00}"
);
var accountId = new AccountId(command.AccountId);
var amount = new Money(command.Amount);
var account =
_repository.GetById(accountId)
?? throw new InvalidOperationException("Account not found.");
Console.WriteLine($"[App] Account loaded. Current balance: {account.Balance.Amount:0.00}");
Console.WriteLine($"[App] Executing withdrawal of {amount.Amount:0.00}...");
account.Withdraw(amount);
Console.WriteLine($"[App] Withdrawal applied. New balance: {account.Balance.Amount:0.00}");
_repository.Save(account);
Console.WriteLine("[App] Account persisted.");
}
}
@@ -0,0 +1,8 @@
namespace CsharpOop.Contracts;
/// Command DTO representing the requested use case
public sealed class WithdrawMoneyCommand
{
public Guid AccountId { get; init; }
public decimal Amount { get; init; }
}
+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);
}
}
+4
View File
@@ -0,0 +1,4 @@
namespace CsharpOop.Domain;
/// Value object used to wrap the aggregate identity
public sealed record AccountId(Guid Value);
+7
View File
@@ -0,0 +1,7 @@
namespace CsharpOop.Domain;
/// Conventional DDD base type for aggregates
public abstract class AggregateRoot<TId>
{
public TId Id { get; protected set; } = default!;
}
+8
View File
@@ -0,0 +1,8 @@
namespace CsharpOop.Domain;
/// Repository abstraction used to load and save accounts
public interface IAccountRepository
{
Account? GetById(AccountId id);
void Save(Account account);
}
+33
View File
@@ -0,0 +1,33 @@
namespace CsharpOop.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);
}
@@ -0,0 +1,27 @@
using CsharpOop.Domain;
namespace CsharpOop.Infrastructure;
/// Simple in-memory repository for demonstration
public class InMemoryAccountRepository : IAccountRepository
{
private readonly Dictionary<AccountId, Account> _accounts = new();
public Account? GetById(AccountId id)
{
var found = _accounts.TryGetValue(id, out var account);
Console.WriteLine(
found ? $"[Repo] Loaded account {id.Value}" : $"[Repo] Account {id.Value} not found"
);
return found ? account : null;
}
public void Save(Account account)
{
_accounts[account.Id] = account;
Console.WriteLine(
$"[Repo] Saved account {account.Id.Value} with balance {account.Balance.Amount:0.00}"
);
}
}
+30
View File
@@ -0,0 +1,30 @@
using CsharpOop.Applications;
using CsharpOop.Contracts;
using CsharpOop.Domain;
using CsharpOop.Infrastructure;
namespace CsharpOop;
public class Program
{
public static void Main()
{
Console.WriteLine("[Program] Starting withdraw money demo...");
var repository = new InMemoryAccountRepository();
var handler = new WithdrawMoneyHandler(repository);
var accountId = Guid.NewGuid();
Console.WriteLine($"[Program] Seeding account {accountId} with opening balance 200.00");
repository.Save(new Account(new AccountId(accountId), new Money(200m)));
var command = new WithdrawMoneyCommand { AccountId = accountId, Amount = 100m };
Console.WriteLine(
$"[Program] Dispatching command: withdraw {command.Amount:0.00} from account {command.AccountId}"
);
handler.Handle(command);
Console.WriteLine("[Program] Demo completed.");
}
}
+9
View File
@@ -0,0 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net10.0</TargetFramework>
<RootNamespace>csharp_oop</RootNamespace>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
</Project>