Console app for Before (ActiveRecord-style) > Add Client.FindByName convenience wrapper
This commit is contained in:
@@ -23,6 +23,27 @@ public class Client : DbBase
|
|||||||
{
|
{
|
||||||
Orders = new ClientOrders(this);
|
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>
|
/// <summary>
|
||||||
|
|||||||
Reference in New Issue
Block a user