order total: TotalInCents on both Orders, mapped into OrderDto and OrderData

yak: ordertotalincents-on-both-sides-dto-mapping-j00p
This commit is contained in:
2026-09-16 18:39:48 +01:00
parent 49f4ca395f
commit 803580786b
8 changed files with 39 additions and 9 deletions
+4
View File
@@ -29,6 +29,9 @@ public sealed class OrderData : DbBase
/// <summary>Free-text description of what this order is for.</summary>
public string Description { get; set; } = string.Empty;
/// <summary>Order total in cents of the stored order.</summary>
public int TotalInCents { get; set; }
/// <summary>
/// Foreign key to the owning client: <see cref="Guid.Empty"/> when the
/// order has no client, or when its client had no id yet at save time —
@@ -101,6 +104,7 @@ public class DbContext
{
var row = (OrderData)existing; // refresh, keep row identity
row.Description = fresh.Description;
row.TotalInCents = fresh.TotalInCents;
row.ClientId = fresh.ClientId;
return;
}
+3
View File
@@ -72,6 +72,7 @@ public static class Mappers
{
Id = order.Id,
Description = order.Description,
TotalInCents = order.TotalInCents,
ClientDto = order.Client is null ? null : MapClient(order.Client, seen),
};
seen[order] = dto;
@@ -102,6 +103,7 @@ public static class Mappers
{
Id = dto.Id,
Description = dto.Description,
TotalInCents = dto.TotalInCents,
Client = dto.ClientDto is null ? null : UnmapClient(dto.ClientDto, seen),
};
seen[dto] = order;
@@ -132,6 +134,7 @@ public static class Mappers
{
Id = order.Id,
Description = order.Description,
TotalInCents = order.TotalInCents,
ClientId = order.Client?.Id ?? Guid.Empty,
};
}
+7
View File
@@ -17,6 +17,13 @@ public class Order
/// <summary>Free-text description of what this order is for.</summary>
public string Description { get; set; } = string.Empty;
/// <summary>
/// Order total in cents (integer — no floating-point money). A normal
/// attribute: it is persisted in <see cref="OrderData"/> and mapped
/// one-on-one into <see cref="OrderDto.TotalInCents"/>.
/// </summary>
public int TotalInCents { get; set; }
/// <summary>
/// The client that owns this order. Set automatically by
/// <see cref="ClientOrders"/> when the order is added to
+3
View File
@@ -13,6 +13,9 @@ public sealed record OrderDto
/// <summary>Free-text description of what this order is for.</summary>
public string Description { get; init; } = string.Empty;
/// <summary>Order total in cents of the underlying order.</summary>
public int TotalInCents { get; init; }
/// <summary>
/// Back-reference to the owning client, or <c>null</c> for a bare order
/// that has not been added to any client.
+4 -4
View File
@@ -18,10 +18,10 @@ DbBase.Context = ctx;
var jane = new Client { Name = "Jane Doe" };
var bob = new Client { Name = "Bob Smith" };
jane.Orders.Add(new Order { Description = "Design consultation" });
jane.Orders.Add(new Order { Description = "Website redesign" });
jane.Orders.Add(new Order { Description = "Design consultation", TotalInCents = 120_00 });
jane.Orders.Add(new Order { Description = "Website redesign", TotalInCents = 750_00 });
bob.Orders.Add(new Order { Description = "Monthly retainer" });
bob.Orders.Add(new Order { Description = "Monthly retainer", TotalInCents = 85_00 });
// Orders need to be attached too so they get IDs:
ctx.Attach(jane.Orders[0]);
@@ -38,7 +38,7 @@ foreach (var client in DbBase.Context!.Tracked.OfType<Client>())
{
Console.WriteLine($"{client.Name} [{client.Id}]");
foreach (var order in client.Orders)
Console.WriteLine($" - order [{order.Id}]: {order.Description}");
Console.WriteLine($" - order [{order.Id}]: {order.Description} ({order.TotalInCents} cents)");
}
// ----- find by name -------------------------------------------------------
+6
View File
@@ -11,6 +11,12 @@ public class Order : DbBase
/// <summary>Free-text description of what this order is for.</summary>
public string Description { get; set; } = string.Empty;
/// <summary>
/// Order total in cents (integer — no floating-point money). Part of the
/// persisted state, like every other scalar.
/// </summary>
public int TotalInCents { get; set; }
/// <summary>
/// The client that owns this order. Set automatically by
/// <see cref="ClientOrders"/> when the order is added to