From 0d02927ba67903ee91f0c77d89ff49f1da021367 Mon Sep 17 00:00:00 2001 From: Oli Sturm Date: Mon, 27 Apr 2026 23:42:31 +0100 Subject: [PATCH] improve output --- csharp-es/Domain/Account.cs | 6 ++++-- csharp-es/Library/Logging.cs | 16 +--------------- csharp-es/Program.cs | 21 +++++++++++++-------- csharp-es/csharp-es.csproj | 3 +++ 4 files changed, 21 insertions(+), 25 deletions(-) diff --git a/csharp-es/Domain/Account.cs b/csharp-es/Domain/Account.cs index 4a744d7..459fe93 100644 --- a/csharp-es/Domain/Account.cs +++ b/csharp-es/Domain/Account.cs @@ -11,6 +11,8 @@ public abstract record AccountCommand public sealed record WithdrawMoney(Guid AccountId, Money Amount) : AccountCommand; public sealed record DepositMoney(Guid AccountId, Money Amount) : AccountCommand; + + public sealed record UnhandledTestCommand() : AccountCommand; } public abstract record AccountEvent(Guid AccountId) @@ -31,7 +33,7 @@ public abstract record AccountError public sealed record OpeningBalanceMustBeNonNegative : AccountError; - public sealed record InnerException(Exception exception) : AccountError; + public sealed record InnerException(string Message) : AccountError; } public static class AccountDecider @@ -75,7 +77,7 @@ public static class AccountDecider _ => throw new InvalidOperationException("Unknown command."), }, - e => new AccountError.InnerException(e) + e => new AccountError.InnerException(e.Message) ); public static Result Evolve( diff --git a/csharp-es/Library/Logging.cs b/csharp-es/Library/Logging.cs index ce58f5f..a3d8af2 100644 --- a/csharp-es/Library/Logging.cs +++ b/csharp-es/Library/Logging.cs @@ -1,6 +1,3 @@ -using System.Text.Json; -using System.Text.Json.Serialization; - namespace CsharpEs.Library; public static class Logging @@ -23,18 +20,7 @@ public static class Logging } public static string Format(Object? o) => - o != null - ? Indent( - JsonSerializer.Serialize( - o, - new JsonSerializerOptions - { - WriteIndented = true, - Converters = { new JsonStringEnumConverter() }, - } - ) - ) - : "(null)"; + Indent(ObjectDumper.Dump(o, new DumpOptions() { DumpStyle = DumpStyle.CSharp })); public static void Output(string src, string message, Object? x) { diff --git a/csharp-es/Program.cs b/csharp-es/Program.cs index 83132dd..c8e2436 100644 --- a/csharp-es/Program.cs +++ b/csharp-es/Program.cs @@ -19,10 +19,13 @@ public class Program { accountId = Guid.NewGuid(); demoCommands = demoCommands.AddRange([ - new AccountCommand.OpenAccount(accountId, new Money(-200m)), - // new AccountCommand.OpenAccount(accountId, new Money(200m)), + // bring this in to see an error right from the start + // new AccountCommand.OpenAccount(accountId, new Money(-200m)), + new AccountCommand.OpenAccount(accountId, new Money(200m)), new AccountCommand.WithdrawMoney(accountId, new Money(100m)), new AccountCommand.WithdrawMoney(accountId, new Money(200m)), + // or maybe an exception in between? + // new AccountCommand.UnhandledTestCommand(), new AccountCommand.DepositMoney(accountId, new Money(500m)), ]); } @@ -65,12 +68,14 @@ public class Program .Aggregate( Result.Ok(null), (stateResult, command) => - stateResult.Bind(state => - AccountDecider - .Decide(state, command) - .MapError(e => (DemoError)new DemoError.Account(e)) - .Bind(@event => ApplyEvent(readModel, state, @event)) - ) + stateResult + .Bind(state => + AccountDecider + .Decide(state, command) + .MapError(e => (DemoError)new DemoError.Account(e)) + .Bind(@event => ApplyEvent(readModel, state, @event)) + ) + .Log("loop", "Intermediate state") ) .Bind(s => readModel diff --git a/csharp-es/csharp-es.csproj b/csharp-es/csharp-es.csproj index a955afb..c4402e9 100644 --- a/csharp-es/csharp-es.csproj +++ b/csharp-es/csharp-es.csproj @@ -6,4 +6,7 @@ enable enable + + +