persistence records carry data: ClientData/OrderData + domain->persistence mappers
The fake DbContext now stores data-only DbBase subclasses (renamed from the identity-only Shadow wrappers) and refreshes rows in place on re-Save; the object graph is flattened to ids in storage. yak: data-carrying-persistence-records-orderdataclientdata-mappers-tb3a
This commit is contained in:
+81
-56
@@ -1,95 +1,120 @@
|
||||
namespace After;
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Internal shadow wrappers — live *inside* the persistence layer so the
|
||||
// domain never sees a DbContext back-reference. Subclassed per-entity-type
|
||||
// solely to make `Set<T>` strongly-typed.
|
||||
// The persistence side of the "after" situation. The (fake) framework stores
|
||||
// its own data-only records — <see cref="ClientData"/> / <see cref="OrderData"/>,
|
||||
// both <see cref="DbBase"/> subclasses — never the domain objects themselves.
|
||||
// Domain state reaches storage only through explicit mapping; the context keeps
|
||||
// a reference-identity map so a second <see cref="Save"/> of the same domain
|
||||
// object refreshes its row instead of duplicating it.
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
/// <summary>Lightweight shadow holder kept alive inside the context.</summary>
|
||||
public abstract class Shadow
|
||||
/// <summary>
|
||||
/// Persisted shape of a <see cref="Client"/>: data only — the orders are
|
||||
/// separate <see cref="OrderData"/> rows, so nothing more is needed here.
|
||||
/// </summary>
|
||||
public sealed class ClientData : DbBase
|
||||
{
|
||||
/// <summary>ID assigned when first persisted.</summary>
|
||||
public Guid Id { get; set; }
|
||||
|
||||
/// <summary>Back-reference to the owning <see cref="DbContext"/> (internal only).</summary>
|
||||
public DbContext? DbContext { get; internal set; }
|
||||
/// <summary>Display name of the client.</summary>
|
||||
public string Name { get; set; } = string.Empty;
|
||||
}
|
||||
|
||||
/// <summary>Shadow holder for a persisted <see cref="Client"/>.</summary>
|
||||
public sealed class ClientShadow : Shadow { /* no extra data */ }
|
||||
/// <summary>
|
||||
/// Persisted shape of an <see cref="Order"/>: data only. The object graph is
|
||||
/// flattened for storage — the back-reference to the client becomes a foreign
|
||||
/// key, because storage has ids, not object references.
|
||||
/// </summary>
|
||||
public sealed class OrderData : DbBase
|
||||
{
|
||||
/// <summary>Free-text description of what this order is for.</summary>
|
||||
public string Description { get; set; } = string.Empty;
|
||||
|
||||
/// <summary>Shadow holder for a persisted <see cref="Order"/>.</summary>
|
||||
public sealed class OrderShadow : Shadow { /* no extra data */ }
|
||||
/// <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 —
|
||||
/// save clients before their orders.
|
||||
/// </summary>
|
||||
public Guid ClientId { get; set; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The fake <c>DbContext</c>, refactored to use per-type shadow collections
|
||||
/// (mirroring EF Core's <c>Set<T></c>) plus an instance-level
|
||||
/// <c>Find<T></c> query method.
|
||||
/// The fake <c>DbContext</c>: per-type tables of data records, mirroring EF
|
||||
/// Core's <c>Set<T></c>. <see cref="Save(Client)"/> / <see cref="Save(Order)"/>
|
||||
/// map the domain object's *current* state into its record — inserting on the
|
||||
/// first save, refreshing in place on later ones.
|
||||
/// </summary>
|
||||
public class DbContext
|
||||
{
|
||||
// ── per-type shadow tables ───────────────────────────────────────
|
||||
private readonly List<ClientShadow> _clients = new();
|
||||
private readonly List<OrderShadow> _orders = new();
|
||||
// ── per-type data tables ─────────────────────────────────────────
|
||||
private readonly List<ClientData> _clients = new();
|
||||
private readonly List<OrderData> _orders = new();
|
||||
|
||||
// ── fast lookup: domain object reference → its shadow ────────────
|
||||
private readonly Dictionary<object, Shadow> _byRef = new();
|
||||
// ── fast lookup: domain object reference → its persisted record ──
|
||||
private readonly Dictionary<object, DbBase> _byRef = new();
|
||||
|
||||
// ── exposed per-type tables (read-only view) ────────────────────
|
||||
public IReadOnlyList<ClientShadow> Clients => _clients.AsReadOnly();
|
||||
public IReadOnlyList<OrderShadow> Orders => _orders.AsReadOnly();
|
||||
// ── exposed per-type tables (read-only view) ─────────────────────
|
||||
public IReadOnlyList<ClientData> Clients => _clients.AsReadOnly();
|
||||
public IReadOnlyList<OrderData> Orders => _orders.AsReadOnly();
|
||||
|
||||
/// <summary>Generic accessor: the strongly-typed "table" for <typeparamref name="T"/>.</summary>
|
||||
public IReadOnlyList<T> Set<T>() where T : Shadow
|
||||
public IReadOnlyList<T> Set<T>() where T : DbBase
|
||||
=> typeof(T) switch
|
||||
{
|
||||
var t when t == typeof(ClientShadow) => (IReadOnlyList<T>)_clients.AsReadOnly(),
|
||||
var t when t == typeof(OrderShadow) => (IReadOnlyList<T>)_orders.AsReadOnly(),
|
||||
_ => throw new NotSupportedException($"No per-type table for {typeof(T)}"),
|
||||
var t when t == typeof(ClientData) => (IReadOnlyList<T>)_clients.AsReadOnly(),
|
||||
var t when t == typeof(OrderData) => (IReadOnlyList<T>)_orders.AsReadOnly(),
|
||||
_ => throw new NotSupportedException($"No per-type table for {typeof(T)}"),
|
||||
};
|
||||
|
||||
// ── explicit persistence ────────────────────────────────────────
|
||||
// ── explicit persistence: map + insert, or refresh ───────────────
|
||||
|
||||
/// <summary>Persist a <see cref="Client">:</summary>
|
||||
/// <summary>
|
||||
/// Persist a <see cref="Client"/>: the first save maps it to a fresh
|
||||
/// <see cref="ClientData"/> row and writes the generated id back into the
|
||||
/// domain object; a later save refreshes that row's data in place.
|
||||
/// </summary>
|
||||
public void Save(Client client)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(client);
|
||||
if (!_byRef.TryAdd(client, new ClientShadow { DbContext = this }))
|
||||
return; // already saved → id unchanged
|
||||
var shadow = (ClientShadow)_byRef[client];
|
||||
var fresh = Mappers.ToClientData(client);
|
||||
if (_byRef.TryGetValue(client, out var existing))
|
||||
{
|
||||
((ClientData)existing).Name = fresh.Name; // refresh, keep row identity
|
||||
return;
|
||||
}
|
||||
var id = Guid.NewGuid();
|
||||
shadow.Id = client.Id = id; // write-back identity
|
||||
fresh.Id = client.Id = id; // write-back identity
|
||||
fresh.DbContext = this;
|
||||
_clients.Add(fresh);
|
||||
_byRef[client] = fresh;
|
||||
}
|
||||
|
||||
/// <summary>Persist an <see cref="Order">.</summary>
|
||||
/// <summary>
|
||||
/// Persist an <see cref="Order"/> (same insert-or-refresh rule as
|
||||
/// <see cref="Save(Client)"/>; save its client first so the foreign key
|
||||
/// can be filled).
|
||||
/// </summary>
|
||||
public void Save(Order order)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(order);
|
||||
if (!_byRef.TryAdd(order, new OrderShadow { DbContext = this }))
|
||||
return; // already saved → id unchanged
|
||||
var shadow = (OrderShadow)_byRef[order];
|
||||
var id = Guid.NewGuid();
|
||||
shadow.Id = order.Id = id; // write-back identity
|
||||
}
|
||||
|
||||
/// <summary>Persist every tracked entity (mirrors EF's <c>SaveChanges</c>).</summary>
|
||||
public int SaveChanges()
|
||||
{
|
||||
var count = 0;
|
||||
foreach (var shadow in _byRef.Values)
|
||||
var fresh = Mappers.ToOrderData(order);
|
||||
if (_byRef.TryGetValue(order, out var existing))
|
||||
{
|
||||
if (shadow.Id != Guid.Empty) continue; // already had an id
|
||||
shadow.Id = Guid.NewGuid();
|
||||
count++;
|
||||
var row = (OrderData)existing; // refresh, keep row identity
|
||||
row.Description = fresh.Description;
|
||||
row.ClientId = fresh.ClientId;
|
||||
return;
|
||||
}
|
||||
return count;
|
||||
var id = Guid.NewGuid();
|
||||
fresh.Id = order.Id = id; // write-back identity
|
||||
fresh.DbContext = this;
|
||||
_orders.Add(fresh);
|
||||
_byRef[order] = fresh;
|
||||
}
|
||||
|
||||
// ── query helpers ───────────────────────────────────────────────
|
||||
|
||||
/// <summary>Search the per-type table for a shadow whose predicate matches.</summary>
|
||||
public T? Find<T>(Func<T, bool> predicate) where T : Shadow
|
||||
/// <summary>Search the per-type table for a record whose predicate matches.</summary>
|
||||
public T? Find<T>(Func<T, bool> predicate) where T : DbBase
|
||||
=> Set<T>().FirstOrDefault(predicate);
|
||||
|
||||
// ── tracking / status ───────────────────────────────────────────
|
||||
@@ -97,6 +122,6 @@ public class DbContext
|
||||
/// <summary>True if <paramref name="entity"/> has been saved through this context.</summary>
|
||||
public bool IsTracked(object entity) => _byRef.ContainsKey(entity);
|
||||
|
||||
/// <summary>All shadow entities this context currently manages.</summary>
|
||||
public IReadOnlyList<Shadow> Tracked => _byRef.Values.ToList();
|
||||
/// <summary>All data records this context currently manages.</summary>
|
||||
public IReadOnlyList<DbBase> Tracked => _byRef.Values.ToList();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user