improve output
This commit is contained in:
@@ -11,6 +11,8 @@ public abstract record AccountCommand
|
|||||||
public sealed record WithdrawMoney(Guid AccountId, Money Amount) : AccountCommand;
|
public sealed record WithdrawMoney(Guid AccountId, Money Amount) : AccountCommand;
|
||||||
|
|
||||||
public sealed record DepositMoney(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)
|
public abstract record AccountEvent(Guid AccountId)
|
||||||
@@ -31,7 +33,7 @@ public abstract record AccountError
|
|||||||
|
|
||||||
public sealed record OpeningBalanceMustBeNonNegative : AccountError;
|
public sealed record OpeningBalanceMustBeNonNegative : AccountError;
|
||||||
|
|
||||||
public sealed record InnerException(Exception exception) : AccountError;
|
public sealed record InnerException(string Message) : AccountError;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static class AccountDecider
|
public static class AccountDecider
|
||||||
@@ -75,7 +77,7 @@ public static class AccountDecider
|
|||||||
|
|
||||||
_ => throw new InvalidOperationException("Unknown command."),
|
_ => throw new InvalidOperationException("Unknown command."),
|
||||||
},
|
},
|
||||||
e => new AccountError.InnerException(e)
|
e => new AccountError.InnerException(e.Message)
|
||||||
);
|
);
|
||||||
|
|
||||||
public static Result<AccountState?, AccountError> Evolve(
|
public static Result<AccountState?, AccountError> Evolve(
|
||||||
|
|||||||
@@ -1,6 +1,3 @@
|
|||||||
using System.Text.Json;
|
|
||||||
using System.Text.Json.Serialization;
|
|
||||||
|
|
||||||
namespace CsharpEs.Library;
|
namespace CsharpEs.Library;
|
||||||
|
|
||||||
public static class Logging
|
public static class Logging
|
||||||
@@ -23,18 +20,7 @@ public static class Logging
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static string Format(Object? o) =>
|
public static string Format(Object? o) =>
|
||||||
o != null
|
Indent(ObjectDumper.Dump(o, new DumpOptions() { DumpStyle = DumpStyle.CSharp }));
|
||||||
? Indent(
|
|
||||||
JsonSerializer.Serialize(
|
|
||||||
o,
|
|
||||||
new JsonSerializerOptions
|
|
||||||
{
|
|
||||||
WriteIndented = true,
|
|
||||||
Converters = { new JsonStringEnumConverter() },
|
|
||||||
}
|
|
||||||
)
|
|
||||||
)
|
|
||||||
: "(null)";
|
|
||||||
|
|
||||||
public static void Output(string src, string message, Object? x)
|
public static void Output(string src, string message, Object? x)
|
||||||
{
|
{
|
||||||
|
|||||||
+13
-8
@@ -19,10 +19,13 @@ public class Program
|
|||||||
{
|
{
|
||||||
accountId = Guid.NewGuid();
|
accountId = Guid.NewGuid();
|
||||||
demoCommands = demoCommands.AddRange([
|
demoCommands = demoCommands.AddRange([
|
||||||
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.OpenAccount(accountId, new Money(200m)),
|
||||||
new AccountCommand.WithdrawMoney(accountId, new Money(100m)),
|
new AccountCommand.WithdrawMoney(accountId, new Money(100m)),
|
||||||
new AccountCommand.WithdrawMoney(accountId, new Money(200m)),
|
new AccountCommand.WithdrawMoney(accountId, new Money(200m)),
|
||||||
|
// or maybe an exception in between?
|
||||||
|
// new AccountCommand.UnhandledTestCommand(),
|
||||||
new AccountCommand.DepositMoney(accountId, new Money(500m)),
|
new AccountCommand.DepositMoney(accountId, new Money(500m)),
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
@@ -65,12 +68,14 @@ public class Program
|
|||||||
.Aggregate(
|
.Aggregate(
|
||||||
Result<AccountState?, DemoError>.Ok(null),
|
Result<AccountState?, DemoError>.Ok(null),
|
||||||
(stateResult, command) =>
|
(stateResult, command) =>
|
||||||
stateResult.Bind(state =>
|
stateResult
|
||||||
AccountDecider
|
.Bind(state =>
|
||||||
.Decide(state, command)
|
AccountDecider
|
||||||
.MapError(e => (DemoError)new DemoError.Account(e))
|
.Decide(state, command)
|
||||||
.Bind(@event => ApplyEvent(readModel, state, @event))
|
.MapError(e => (DemoError)new DemoError.Account(e))
|
||||||
)
|
.Bind(@event => ApplyEvent(readModel, state, @event))
|
||||||
|
)
|
||||||
|
.Log("loop", "Intermediate state")
|
||||||
)
|
)
|
||||||
.Bind(s =>
|
.Bind(s =>
|
||||||
readModel
|
readModel
|
||||||
|
|||||||
@@ -6,4 +6,7 @@
|
|||||||
<ImplicitUsings>enable</ImplicitUsings>
|
<ImplicitUsings>enable</ImplicitUsings>
|
||||||
<Nullable>enable</Nullable>
|
<Nullable>enable</Nullable>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
<ItemGroup>
|
||||||
|
<PackageReference Include="ObjectDumper.NET" Version="4.3.2" />
|
||||||
|
</ItemGroup>
|
||||||
</Project>
|
</Project>
|
||||||
|
|||||||
Reference in New Issue
Block a user