Doc comments, inline comments, and README no longer editorialize about the code (ActiveRecord, leak, fake, Yak before/after). They now describe behavior only. Test A_client_can_reach_its_DbContext__the_leak renamed to A_client_can_reach_its_DbContext. No behavior changes; all 27 tests pass.
75 lines
2.4 KiB
C#
75 lines
2.4 KiB
C#
using Before;
|
|
|
|
// Before.Console — seeds clients and orders, saves them through the
|
|
// DbContext, and queries them via the Client finders and DbBase.Context.
|
|
|
|
var ctx = new DbContext();
|
|
DbBase.Context = ctx;
|
|
|
|
// ----- seed data ----------------------------------------------------------
|
|
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" });
|
|
|
|
bob.Orders.Add(new Order { Description = "Monthly retainer" });
|
|
|
|
// Orders are attached separately so they get IDs:
|
|
ctx.Attach(jane.Orders[0]);
|
|
ctx.Attach(jane.Orders[1]);
|
|
ctx.Attach(bob.Orders[0]);
|
|
|
|
ctx.Attach(jane);
|
|
ctx.Attach(bob);
|
|
ctx.Save();
|
|
|
|
// ----- list all clients ---------------------------------------------------
|
|
Console.WriteLine("=== All Clients ===");
|
|
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}");
|
|
}
|
|
|
|
// ----- find by name -------------------------------------------------------
|
|
Console.WriteLine("\n=== Find by Name (null-returning) ===");
|
|
var found = Client.FindByName("Jane Doe");
|
|
if (found is not null)
|
|
{
|
|
Console.WriteLine($"Found: {found.Name} [{found.Id}]");
|
|
Console.WriteLine($" Orders: {found.Orders.Count}");
|
|
}
|
|
else
|
|
{
|
|
Console.WriteLine("Not found.");
|
|
}
|
|
|
|
// ----- find by name (throwing variant) ------------------------------------
|
|
Console.WriteLine("\n=== Find by Name Required (throws instead of returning null) ===");
|
|
try
|
|
{
|
|
var required = Client.FindByNameRequired("Jane Doe");
|
|
Console.WriteLine($"Found: {required.Name} [{required.Id}]");
|
|
Console.WriteLine($" Orders: {required.Orders.Count}");
|
|
}
|
|
catch (ObjectNotFoundException ex)
|
|
{
|
|
Console.WriteLine($"Not found: {ex.Message}");
|
|
}
|
|
|
|
// ----- a name that matches nothing ----------------------------------------
|
|
Console.WriteLine("\n=== Miss: null-returning vs throwing ===");
|
|
var missNull = Client.FindByName("Nobody Here");
|
|
Console.WriteLine($"FindByName(\"Nobody Here\"): {(missNull == null ? "null" : missNull.Name)}");
|
|
try
|
|
{
|
|
Client.FindByNameRequired("Nobody Here");
|
|
}
|
|
catch (ObjectNotFoundException ex)
|
|
{
|
|
var msg = ex.Message.ReplaceLineEndings(" ").Trim();
|
|
Console.WriteLine($"FindByNameRequired(\"Nobody Here\"): throws - {msg}");
|
|
}
|