Add FindByNameRequired throwing variant (yak: Add FindByName! throwing variant)
This commit is contained in:
@@ -42,7 +42,7 @@ foreach (var client in DbBase.Context!.Tracked.OfType<Client>())
|
||||
}
|
||||
|
||||
// ----- find by name -------------------------------------------------------
|
||||
Console.WriteLine("\n=== Find by Name ===");
|
||||
Console.WriteLine("\n=== Find by Name (null-returning) ===");
|
||||
// No context argument: FindByName falls back to the DbBase.Context singleton.
|
||||
// This is the ActiveRecord ergonomic — call sites don't carry the context,
|
||||
// but the null that comes back on a miss can't tell "no such client" from
|
||||
@@ -57,3 +57,30 @@ 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 (InvalidOperationException ex)
|
||||
{
|
||||
Console.WriteLine($"Not found: {ex.Message}");
|
||||
}
|
||||
|
||||
// Show the contrast with a non-existent name:
|
||||
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 (InvalidOperationException ex)
|
||||
{
|
||||
var msg = ex.Message.ReplaceLineEndings(" ").Trim();
|
||||
Console.WriteLine($"FindByNameRequired(\"Nobody Here\"): throws - {msg}");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user