fix project names
This commit is contained in:
@@ -1,17 +0,0 @@
|
||||
using CsharpFp2.Domain;
|
||||
|
||||
namespace CsharpFp2.Application;
|
||||
|
||||
public static class AccountApplicationService
|
||||
{
|
||||
public static void WithdrawMoney(IAccountRepository repository, Guid accountId, decimal amount)
|
||||
{
|
||||
var account =
|
||||
repository.GetById(accountId)
|
||||
?? throw new InvalidOperationException("Account not found.");
|
||||
|
||||
account.Withdraw(new Money(amount));
|
||||
|
||||
repository.Save(account);
|
||||
}
|
||||
}
|
||||
@@ -1,37 +0,0 @@
|
||||
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);
|
||||
}
|
||||
}
|
||||
@@ -1,8 +0,0 @@
|
||||
namespace CsharpFp2.Domain;
|
||||
|
||||
/// Repository abstraction used to load and save accounts
|
||||
public interface IAccountRepository
|
||||
{
|
||||
Account? GetById(Guid id);
|
||||
void Save(Account account);
|
||||
}
|
||||
@@ -1,17 +0,0 @@
|
||||
namespace CsharpFp2.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;
|
||||
}
|
||||
}
|
||||
@@ -1,33 +0,0 @@
|
||||
namespace CsharpFp2.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);
|
||||
}
|
||||
@@ -1,25 +0,0 @@
|
||||
using CsharpFp2.Domain;
|
||||
|
||||
namespace CsharpFp2.Infrastructure;
|
||||
|
||||
/// Simple in-memory repository for demonstration
|
||||
public class InMemoryAccountRepository : IAccountRepository
|
||||
{
|
||||
private readonly Dictionary<Guid, Account> _accounts = new();
|
||||
|
||||
public Account? GetById(Guid id)
|
||||
{
|
||||
var found = _accounts.TryGetValue(id, out var account);
|
||||
Console.WriteLine(found ? $"[Repo] Loaded account {id}" : $"[Repo] Account {id} not found");
|
||||
|
||||
return found ? account : null;
|
||||
}
|
||||
|
||||
public void Save(Account account)
|
||||
{
|
||||
_accounts[account.Id] = account;
|
||||
Console.WriteLine(
|
||||
$"[Repo] Saved account {account.Id} with balance {account.Balance.Amount:0.00}"
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -1,27 +0,0 @@
|
||||
using CsharpFp2.Application;
|
||||
using CsharpFp2.Domain;
|
||||
using CsharpFp2.Infrastructure;
|
||||
|
||||
namespace CsharpFp2;
|
||||
|
||||
public class Program
|
||||
{
|
||||
public static void Main()
|
||||
{
|
||||
Console.WriteLine("[csharp-fp2] Starting withdraw money demo...");
|
||||
|
||||
var repository = new InMemoryAccountRepository();
|
||||
|
||||
var accountId = Guid.NewGuid();
|
||||
Console.WriteLine($"[csharp-fp2] Seeding account {accountId} with opening balance 200.00");
|
||||
repository.Save(new Account(accountId, new Money(200m)));
|
||||
|
||||
decimal amount = 100m;
|
||||
Console.WriteLine(
|
||||
$"[csharp-fp2] Executing withdrawal {amount:0.00} from account {accountId}"
|
||||
);
|
||||
AccountApplicationService.WithdrawMoney(repository, accountId, amount);
|
||||
|
||||
Console.WriteLine("[csharp-fp2] Demo completed.");
|
||||
}
|
||||
}
|
||||
@@ -1,9 +0,0 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net10.0</TargetFramework>
|
||||
<RootNamespace>csharp_fp2</RootNamespace>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
</Project>
|
||||
Reference in New Issue
Block a user