Exercise 2 solution: OrderLines in the domain, edges unchanged

OrderLine + computed TotalInCents; both inbound mappers reconstruct a
one-line order from the flat DTO/row (derived data is re-derived, not
written); builder realises WithTotal as a single line. Outbound mappers,
DTO, OrderData, DbContext and WebApp untouched — the small-steps payoff.

yak: exercise-2-solution-on-branch-exercise-2-solution-hw7e
This commit is contained in:
2026-09-17 08:56:14 +01:00
parent 89a1158c93
commit 46b91fc3ce
6 changed files with 99 additions and 12 deletions
@@ -16,6 +16,7 @@ namespace BeforeAfter.Tests.TestData;
public sealed class OrderBuilder
{
private readonly Order _order = new();
private int? _totalInCents;
/// <summary>A valid, fresh order — the neutral starting point for tests.</summary>
public static OrderBuilder ValidOrder() => new();
@@ -33,15 +34,23 @@ public sealed class OrderBuilder
}
/// <summary>
/// Sets the total in cents (<c>150_00</c> = €150.00). How the requested
/// total is realised inside the order is an implementation detail that may
/// change as the domain evolves; this API stays.
/// Sets the total in cents (<c>150_00</c> = €150.00). Since OrderLines
/// arrived, the total is computed — the requested total is realised in
/// <see cref="Build"/> as a single line carrying the order's description,
/// so <c>WithDescription</c>/<c>WithTotal</c> work in any order. This API
/// stayed identical across that domain evolution; only these internals
/// changed.
/// </summary>
public OrderBuilder WithTotal(int totalInCents)
{
_order.TotalInCents = totalInCents;
_totalInCents = totalInCents;
return this;
}
public Order Build() => _order;
public Order Build()
{
if (_totalInCents is int total)
_order.Lines.Add(new OrderLine(_order.Description, total));
return _order;
}
}