Console app for Before (ActiveRecord-style) > Add Client.FindByName convenience wrapper

This commit is contained in:
2026-09-14 20:43:47 +01:00
parent cdadafe676
commit 92b9e3f784
+21
View File
@@ -23,6 +23,27 @@ public class Client : DbBase
{
Orders = new ClientOrders(this);
}
/// <summary>
/// Convenience lookup: walks the <paramref name="db"/>'s tracked entities,
/// finds the first <see cref="Client"/> whose <see cref="Name"/>
/// equals <paramref name="name"/>. Returns <c>null</c> when no match.
///
/// This mirrors how an ActiveRecord-style ORM might surface a static
/// finder on the domain class itself — it rides the <c>DbContext</c> leak
/// from the entity's back-reference.
/// </summary>
public static Client? FindByName(string name, DbContext? db)
{
if (db is null)
return null;
foreach (var e in db.Tracked)
if (e is Client c && c.Name == name)
return c;
return null;
}
}
/// <summary>