Files
mostalive 46b91fc3ce 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
2026-09-17 08:56:14 +01:00

57 lines
1.8 KiB
C#

using After;
namespace BeforeAfter.Tests.TestData;
/// <summary>
/// Test data builder for an <see cref="Order"/> (TestDataBuilder pattern).
/// <see cref="ValidOrder"/> starts from a <em>fresh</em> order —
/// <see cref="Guid.Empty"/> id, empty description, zero total, exactly what
/// <c>new Order()</c> gives — and every <c>With...</c> returns the builder for
/// chaining; <see cref="Build"/> hands back the order.
///
/// This class is the only place tests touch an order's *construction*: when
/// the domain evolves (e.g. OrderLines making the total computed), only these
/// internals change and the tests keep reading the same.
/// </summary>
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();
public OrderBuilder WithId(Guid id)
{
_order.Id = id;
return this;
}
public OrderBuilder WithDescription(string description)
{
_order.Description = description;
return this;
}
/// <summary>
/// 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)
{
_totalInCents = totalInCents;
return this;
}
public Order Build()
{
if (_totalInCents is int total)
_order.Lines.Add(new OrderLine(_order.Description, total));
return _order;
}
}