Files
demo-ddd-without-oop/csharp-fp1/Infrastructure/InMemoryAccountRepository.cs
T
2026-04-22 13:38:09 +01:00

41 lines
1.1 KiB
C#

using CsharpFp1.Domain;
namespace CsharpFp1.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 really miss the interface idea, 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);
}
}