This commit is contained in:
@@ -0,0 +1,46 @@
|
||||
using Domain;
|
||||
|
||||
namespace Db;
|
||||
|
||||
/// <summary>Hand-written mappers between domain objects and persistence records; each method maps one direction and never mutates its input.</summary>
|
||||
public static class DataMappers
|
||||
{
|
||||
/// <summary>Map a <see cref="Client"/> to its persistence record <see cref="ClientData"/>.</summary>
|
||||
public static ClientData ToClientData(Client client)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(client);
|
||||
return new ClientData { Id = client.Id, Name = client.Name };
|
||||
}
|
||||
|
||||
/// <summary>Map an <see cref="Order"/> to its persistence record <see cref="OrderData"/>; the client back-reference becomes a foreign key.</summary>
|
||||
public static OrderData ToOrderData(Order order)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(order);
|
||||
return new OrderData
|
||||
{
|
||||
Id = order.Id,
|
||||
Description = order.Description,
|
||||
TotalInCents = order.TotalInCents.Cents, // value object → plain int for the row
|
||||
ClientId = order.Client?.Id ?? Guid.Empty,
|
||||
};
|
||||
}
|
||||
|
||||
/// <summary>Map a <see cref="ClientData"/> record back to a <see cref="Client"/>.</summary>
|
||||
public static Client FromClientData(ClientData row)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(row);
|
||||
return new Client { Id = row.Id, Name = row.Name };
|
||||
}
|
||||
|
||||
/// <summary>Map an <see cref="OrderData"/> record back to an <see cref="Order"/>; the context reassembles the graph.</summary>
|
||||
public static Order FromOrderData(OrderData row)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(row);
|
||||
return new Order
|
||||
{
|
||||
Id = row.Id,
|
||||
Description = row.Description,
|
||||
TotalInCents = OrderMoney.From(row.TotalInCents), // plain int from the row → value object
|
||||
};
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user