add fp2 sample
This commit is contained in:
@@ -0,0 +1,46 @@
|
||||
using CsharpFp2.Domain;
|
||||
using CsharpFp2.Infrastructure;
|
||||
|
||||
namespace CsharpFp2.Application;
|
||||
|
||||
// delegate type is optional, but nice to illustrate
|
||||
public delegate WithdrawResult WithdrawMoney(Guid accountId, decimal amount);
|
||||
|
||||
public static class AccountApplication
|
||||
{
|
||||
public static WithdrawMoney CreateWithdrawMoney(
|
||||
LoadAccount loadAccount,
|
||||
SaveAccount saveAccount
|
||||
)
|
||||
{
|
||||
return (accountId, amount) =>
|
||||
{
|
||||
var account =
|
||||
loadAccount(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:0.00}...");
|
||||
|
||||
switch (AccountDomain.Withdraw(account, new Money(amount)))
|
||||
{
|
||||
case WithdrawResult.Success s:
|
||||
{
|
||||
Console.WriteLine(
|
||||
$"[App] Withdrawal applied. New balance: {s.Account.Balance.Amount:0.00}"
|
||||
);
|
||||
saveAccount(s.Account);
|
||||
Console.WriteLine("[App] Account persisted.");
|
||||
return s;
|
||||
}
|
||||
|
||||
case var other:
|
||||
{
|
||||
Console.Error.WriteLine($"[App Error] Withdrawal failed. Error: {other}");
|
||||
return other;
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
namespace CsharpFp2.Domain;
|
||||
|
||||
public sealed record Account(Guid Id, Money Balance);
|
||||
|
||||
public abstract record WithdrawResult
|
||||
{
|
||||
public sealed record Success(Account Account) : WithdrawResult;
|
||||
|
||||
public sealed record AmountMustBePositive : WithdrawResult;
|
||||
|
||||
public sealed record InsufficientBalance(Money balance, Money amount) : WithdrawResult;
|
||||
}
|
||||
|
||||
public static class AccountDomain
|
||||
{
|
||||
public static Account Open(Guid id, Money openingBalance)
|
||||
{
|
||||
// Assuming we don't intend to allow invalid open balances to be passed
|
||||
// here, we can view this as a technical (i.e. dev-time) error and
|
||||
// stick to an exception.
|
||||
if (openingBalance.Amount < 0)
|
||||
throw new ArgumentOutOfRangeException(nameof(openingBalance));
|
||||
|
||||
return new Account(id, openingBalance);
|
||||
}
|
||||
|
||||
public static WithdrawResult Withdraw(Account account, Money amount)
|
||||
{
|
||||
if (amount.Amount <= 0)
|
||||
return new WithdrawResult.AmountMustBePositive();
|
||||
|
||||
if (account.Balance.Amount < amount.Amount)
|
||||
return new WithdrawResult.InsufficientBalance(account.Balance, amount);
|
||||
|
||||
return new WithdrawResult.Success(
|
||||
account with
|
||||
{
|
||||
Balance = account.Balance.Subtract(amount),
|
||||
}
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
namespace CsharpFp2.Domain;
|
||||
|
||||
public sealed record Money(decimal Amount)
|
||||
{
|
||||
public Money Add(Money other) => new(Amount + other.Amount);
|
||||
|
||||
public Money Subtract(Money other) => new(Amount - other.Amount);
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
using CsharpFp2.Domain;
|
||||
|
||||
namespace CsharpFp2.Infrastructure;
|
||||
|
||||
// If we don't like working with generic delegates directly, we can
|
||||
// create custom named delegate types.
|
||||
public delegate Account? LoadAccount(Guid id);
|
||||
|
||||
public delegate void SaveAccount(Account accunt);
|
||||
|
||||
// If we don't want to use tuples, or you really miss the idea of a combined "interface",
|
||||
// we can create a named container
|
||||
// public sealed record AccountPersistence(LoadAccount Load, SaveAccount Save);
|
||||
|
||||
public static class InMemoryAccount
|
||||
{
|
||||
public static (LoadAccount, SaveAccount) Create()
|
||||
{
|
||||
Dictionary<Guid, Account> store = new();
|
||||
|
||||
Account? GetById(Guid id)
|
||||
{
|
||||
var found = store.TryGetValue(id, out var account);
|
||||
Console.WriteLine(
|
||||
found ? $"[Repo] Loaded account {id}" : $"[Repo] Account {id} not found"
|
||||
);
|
||||
|
||||
return found ? account : null;
|
||||
}
|
||||
|
||||
void Save(Account account)
|
||||
{
|
||||
store[account.Id] = account;
|
||||
Console.WriteLine(
|
||||
$"[Repo] Saved account {account.Id} with balance {account.Balance.Amount:0.00}"
|
||||
);
|
||||
}
|
||||
|
||||
return (GetById, Save);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
using CsharpFp2.Application;
|
||||
using CsharpFp2.Domain;
|
||||
using CsharpFp2.Infrastructure;
|
||||
|
||||
Console.WriteLine("[csharp-fp2] Starting withdraw money demo...");
|
||||
|
||||
var (loadAccount, saveAccount) = InMemoryAccount.Create();
|
||||
var withdrawMoney = AccountApplication.CreateWithdrawMoney(loadAccount, saveAccount);
|
||||
|
||||
var accountId = Guid.NewGuid();
|
||||
Console.WriteLine($"[csharp-fp2] Seeding account {accountId} with opening balance 200.00");
|
||||
saveAccount(new Account(accountId, new Money(200m)));
|
||||
|
||||
decimal amount = 100m;
|
||||
Console.WriteLine($"[csharp-fp2] Executing withdrawal {amount:0.00} from account {accountId}");
|
||||
withdrawMoney(accountId, amount);
|
||||
|
||||
Console.WriteLine("[csharp-fp2] Demo completed.");
|
||||
@@ -0,0 +1,9 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net10.0</TargetFramework>
|
||||
<RootNamespace>CsharpFp2</RootNamespace>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
</Project>
|
||||
Reference in New Issue
Block a user