- DbBase.FindRequired / Client.FindByNameRequired now throw the
domain-specific ObjectNotFoundException (NHibernate lineage; Rails
equivalent: ActiveRecord::RecordNotFound) on a lookup miss; the
no-context case stays InvalidOperationException (configuration error)
- Console demo catches the new type; tests (18)/(21) assert it,
(19)/(23) keep InvalidOperationException with rationale comments
- scripts/run-console.sh delegates to dotnet.sh; run-console.ps1 is
the Windows/PowerShell counterpart (mise-first, repo-root cd)
Yak: objectnotfoundexception-run-console-wrappers-3p8b
DbBase.Find<T> now uses db ?? Context, and Client.FindByName's context
parameter becomes optional — call sites read like Client.FindByName("Jane
Doe") without context threading. A null result now overwhelmingly means
"no match"; the residual "no context configured at all" ambiguity is
documented in remarks as part of the ActiveRecord-leak cost this exercise
illustrates.
Refactor DbContext to use per-type internal storage (Dictionary<Type, Collection>)
instead of a single flat List<DbBase>, mirroring EF Core's DbSet<T> pattern.
Changes:
- Replace _tracked List<DbBase> with _tables Dictionary<Type, Collection<DbBase>>
- Add Table<T>() generic helper for compile-time known types (Find<T>)
- Add TableFor(Type) non-generic helper for Attach where type is only known at runtime
- Add instance-level Find<T>(Predicate<T>) method on DbContext that searches only
the per-type table for T (mirroring DbSet<T>.Find behavior)
- Keep Tracked { get } as a flattened view across all per-type tables (API compatible)
- IsTracked now walks all per-type tables
Scaffold Before.Console project referencing the Before library.
Implements a straight-line demo that creates clients/orders, saves
them via DbBase.Context (singleton), lists all clients with orders,
and finds a client by name using Client.FindByName().
ActiveRecord pattern: expose a shared DbContext on DbBase so any
entity subclass can reach it without passing context through parameters.
The instance-level DbContext back-reference still works; the static one
provides a class-level global fallback.
Refactor After.DbContext from a single flat _tracked dictionary into:
- Per-type shadow collections (_clients, _orders) mirroring EF Core's
Set<T> pattern, each holding type-specific Shadow subtypes
- Public Clients/Orders properties as read-only typed table accessors
- Generic Set<T>() accessor for any Shadow subtype
- Find<T>(Func<T, bool>) query method on the context instance
Internal details:
- Shadow base class with Id + DbContext back-reference (internal set)
- ClientShadow / OrderShadow concrete subtypes per entity
- _byRef Dictionary<object, Shadow> for fast domain→shadow lookup
- SaveChanges() persists all tracked entities at once
After/: plain Client/Order (no DbBase), ClientDto/OrderDto records
carrying the relations both ways, hand-written Mappers with a
reference-identity cache for the back-reference cycle, DbContext with
explicit Save(domain) via shadow entities (flipped dependency), and a
WebApp that consumes only DTOs. AfterTests cover the 7 required
behaviours; full solution builds with 0 warnings, 26/26 tests pass.
Add the C# BEFORE situation: a fake-EF DbContext + DbBase that Client/Order
inherit, the active-record DbContext back-reference (the dependency leak),
navigation fix-up on Client.Orders, and a WebApp that consumes the domain
classes directly. Adds an empty After project so the solution shape is final,
and an xUnit BeforeTests suite covering the required assertions.
- db-subclass-to-dto.sln (classic .sln; SDK 10 defaults to .slnx which breaks run-tests.sh)
- src/Before: DbBase, DbContext, Client (+ClientOrders fix-up), Order, WebApp
- src/After: empty placeholder (filled in Yak 02)
- tests/BeforeAfter.Tests/BeforeTests.cs: 8 tests (7 required + 1 companion)