|
| 1 | +// Generated by Own.TypedBuilder from Order.cs. Do not edit: change Order.cs and regenerate. |
| 2 | +// |
| 3 | +// The state protocol of Order: one [ProtocolToken] per state, one transition per |
| 4 | +// [Transition], one [ProtocolRegion] per state with a way out, the checked refinement, |
| 5 | +// the strict storage of the state, and the staged builder. Own.NET analyses this file as |
| 6 | +// source: it is the protocol's trusted definition surface. |
| 7 | + |
| 8 | +using System; |
| 9 | + |
| 10 | +namespace OrderBackend.Domain; |
| 11 | + |
| 12 | +partial class Order |
| 13 | +{ |
| 14 | + // Draft -> Submit -> Submitted |
| 15 | + internal void ApplySubmit(DateTime at) |
| 16 | + { |
| 17 | + OnSubmit(at); |
| 18 | + Status = OrderStatus.Submitted; |
| 19 | + } |
| 20 | + |
| 21 | + // Submitted -> Approve -> Approved |
| 22 | + internal void ApplyApprove(DateTime at) |
| 23 | + { |
| 24 | + OnApprove(at); |
| 25 | + Status = OrderStatus.Approved; |
| 26 | + } |
| 27 | + |
| 28 | + // Approved -> Ship -> Shipped |
| 29 | + internal void ApplyShip(DateTime at, int trackingNumber) |
| 30 | + { |
| 31 | + OnShip(at, trackingNumber); |
| 32 | + Status = OrderStatus.Shipped; |
| 33 | + } |
| 34 | + |
| 35 | + /// Starts a new Order in its initial state, Draft. Build() exists only once every |
| 36 | + /// required field was given. |
| 37 | + public static DraftBuilder.CustomerStep Create() => new(); |
| 38 | + |
| 39 | + public sealed class DraftBuilder |
| 40 | + { |
| 41 | + private readonly string _customer; |
| 42 | + |
| 43 | + private DraftBuilder(string customer) |
| 44 | + { |
| 45 | + _customer = customer; |
| 46 | + } |
| 47 | + |
| 48 | + public Order Build() => new() { Customer = _customer, Status = OrderStatus.Draft }; |
| 49 | + |
| 50 | + public sealed class CustomerStep |
| 51 | + { |
| 52 | + internal CustomerStep() |
| 53 | + { |
| 54 | + } |
| 55 | + |
| 56 | + public DraftBuilder Customer(string customer) |
| 57 | + { |
| 58 | + ArgumentNullException.ThrowIfNull(customer); |
| 59 | + return new DraftBuilder(customer); |
| 60 | + } |
| 61 | + } |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +/// The runtime half of the refinement: the state came from data, so it is checked once, |
| 66 | +/// at the region entry. |
| 67 | +public sealed class InvalidOrderStateException(int id, OrderStatus actual, OrderStatus required) |
| 68 | + : InvalidOperationException($"order {id} is {actual}, not {required}") |
| 69 | +{ |
| 70 | + public int Id { get; } = id; |
| 71 | + |
| 72 | + public OrderStatus Actual { get; } = actual; |
| 73 | + |
| 74 | + public OrderStatus Required { get; } = required; |
| 75 | +} |
| 76 | + |
| 77 | +/// A persisted state that is not exactly one of OrderStatus's names. Never mapped to a |
| 78 | +/// state: no default, no case folding, no number. |
| 79 | +public sealed class CorruptOrderStateException(string raw) |
| 80 | + : InvalidOperationException($"the persisted order state '{raw}' is not an OrderStatus") |
| 81 | +{ |
| 82 | + public string Raw { get; } = raw; |
| 83 | +} |
| 84 | + |
| 85 | +/// The one mapping between OrderStatus and its stored text: the exact member name. |
| 86 | +public static class OrderStatusStorage |
| 87 | +{ |
| 88 | + public static string ToStore(OrderStatus state) => state switch |
| 89 | + { |
| 90 | + OrderStatus.Draft => "Draft", |
| 91 | + OrderStatus.Submitted => "Submitted", |
| 92 | + OrderStatus.Approved => "Approved", |
| 93 | + OrderStatus.Shipped => "Shipped", |
| 94 | + _ => throw new CorruptOrderStateException(state.ToString()), |
| 95 | + }; |
| 96 | + |
| 97 | + public static OrderStatus FromStore(string raw) => |
| 98 | + TryFromStore(raw, out var state) ? state : throw new CorruptOrderStateException(raw); |
| 99 | + |
| 100 | + public static bool TryFromStore(string? raw, out OrderStatus state) |
| 101 | + { |
| 102 | + switch (raw) |
| 103 | + { |
| 104 | + case "Draft": |
| 105 | + state = OrderStatus.Draft; |
| 106 | + return true; |
| 107 | + case "Submitted": |
| 108 | + state = OrderStatus.Submitted; |
| 109 | + return true; |
| 110 | + case "Approved": |
| 111 | + state = OrderStatus.Approved; |
| 112 | + return true; |
| 113 | + case "Shipped": |
| 114 | + state = OrderStatus.Shipped; |
| 115 | + return true; |
| 116 | + default: |
| 117 | + state = default; |
| 118 | + return false; |
| 119 | + } |
| 120 | + } |
| 121 | +} |
| 122 | + |
| 123 | +/// Order in state Draft. A transition spends this token and hands back the next one. |
| 124 | +[ProtocolToken] |
| 125 | +public readonly ref struct DraftOrder |
| 126 | +{ |
| 127 | + private readonly Order _order; |
| 128 | + |
| 129 | + internal DraftOrder(Order order) => _order = order; |
| 130 | + |
| 131 | + public int Id => _order.Id; |
| 132 | + |
| 133 | + public SubmittedOrder Submit(DateTime at) |
| 134 | + { |
| 135 | + _order.ApplySubmit(at); |
| 136 | + return new SubmittedOrder(_order); |
| 137 | + } |
| 138 | +} |
| 139 | + |
| 140 | +/// Order in state Submitted. A transition spends this token and hands back the next one. |
| 141 | +[ProtocolToken] |
| 142 | +public readonly ref struct SubmittedOrder |
| 143 | +{ |
| 144 | + private readonly Order _order; |
| 145 | + |
| 146 | + internal SubmittedOrder(Order order) => _order = order; |
| 147 | + |
| 148 | + public int Id => _order.Id; |
| 149 | + |
| 150 | + public ApprovedOrder Approve(DateTime at) |
| 151 | + { |
| 152 | + _order.ApplyApprove(at); |
| 153 | + return new ApprovedOrder(_order); |
| 154 | + } |
| 155 | +} |
| 156 | + |
| 157 | +/// Order in state Approved. A transition spends this token and hands back the next one. |
| 158 | +[ProtocolToken] |
| 159 | +public readonly ref struct ApprovedOrder |
| 160 | +{ |
| 161 | + private readonly Order _order; |
| 162 | + |
| 163 | + internal ApprovedOrder(Order order) => _order = order; |
| 164 | + |
| 165 | + public int Id => _order.Id; |
| 166 | + |
| 167 | + public ShippedOrder Ship(DateTime at, int trackingNumber) |
| 168 | + { |
| 169 | + _order.ApplyShip(at, trackingNumber); |
| 170 | + return new ShippedOrder(_order); |
| 171 | + } |
| 172 | +} |
| 173 | + |
| 174 | +/// Order in state Shipped. Terminal: no transition leaves it. |
| 175 | +[ProtocolToken] |
| 176 | +public readonly ref struct ShippedOrder |
| 177 | +{ |
| 178 | + private readonly Order _order; |
| 179 | + |
| 180 | + internal ShippedOrder(Order order) => _order = order; |
| 181 | + |
| 182 | + public int Id => _order.Id; |
| 183 | +} |
| 184 | + |
| 185 | +public delegate void DraftRegion(DraftOrder draft); |
| 186 | +public delegate void SubmittedRegion(SubmittedOrder submitted); |
| 187 | +public delegate void ApprovedRegion(ApprovedOrder approved); |
| 188 | + |
| 189 | +/// The checked refinement: an Order whose state is only known at run time becomes a token |
| 190 | +/// inside the callback, or the call throws and no token exists. A state no transition |
| 191 | +/// leaves has no region: its token could never be spent. |
| 192 | +public static class OrderProtocol |
| 193 | +{ |
| 194 | + [ProtocolRegion] |
| 195 | + public static void WithDraft(Order order, DraftRegion body) |
| 196 | + { |
| 197 | + ArgumentNullException.ThrowIfNull(order); |
| 198 | + ArgumentNullException.ThrowIfNull(body); |
| 199 | + if (order.Status != OrderStatus.Draft) |
| 200 | + throw new InvalidOrderStateException(order.Id, order.Status, OrderStatus.Draft); |
| 201 | + body(new DraftOrder(order)); |
| 202 | + } |
| 203 | + |
| 204 | + [ProtocolRegion] |
| 205 | + public static void WithSubmitted(Order order, SubmittedRegion body) |
| 206 | + { |
| 207 | + ArgumentNullException.ThrowIfNull(order); |
| 208 | + ArgumentNullException.ThrowIfNull(body); |
| 209 | + if (order.Status != OrderStatus.Submitted) |
| 210 | + throw new InvalidOrderStateException(order.Id, order.Status, OrderStatus.Submitted); |
| 211 | + body(new SubmittedOrder(order)); |
| 212 | + } |
| 213 | + |
| 214 | + [ProtocolRegion] |
| 215 | + public static void WithApproved(Order order, ApprovedRegion body) |
| 216 | + { |
| 217 | + ArgumentNullException.ThrowIfNull(order); |
| 218 | + ArgumentNullException.ThrowIfNull(body); |
| 219 | + if (order.Status != OrderStatus.Approved) |
| 220 | + throw new InvalidOrderStateException(order.Id, order.Status, OrderStatus.Approved); |
| 221 | + body(new ApprovedOrder(order)); |
| 222 | + } |
| 223 | +} |
0 commit comments