Find falls back to DbBase.Context singleton when no context passed
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.
This commit is contained in:
@@ -229,7 +229,8 @@ public class BeforeTests
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------
|
||||
// (13) DbBase.Find handles a null context gracefully.
|
||||
// (13) DbBase.Find handles a null context gracefully — with no explicit
|
||||
// context AND no singleton configured, it returns default (null).
|
||||
// ---------------------------------------------------------------------
|
||||
[Fact]
|
||||
public void DbBase_Find_with_null_context_returns_default()
|
||||
@@ -238,6 +239,29 @@ public class BeforeTests
|
||||
Assert.Null(result);
|
||||
}
|
||||
|
||||
// (13b) When no explicit context is passed, Find falls back to the
|
||||
// DbBase.Context singleton instead of returning null.
|
||||
[Fact]
|
||||
public void DbBase_Find_falls_back_to_Context_singleton_when_db_is_null()
|
||||
{
|
||||
var db = new DbContext();
|
||||
var acme = new Client { Name = "SingletonCo" };
|
||||
db.Attach(acme);
|
||||
|
||||
DbBase.Context = db;
|
||||
try
|
||||
{
|
||||
var result = DbBase.Find<Client>(c => c.Name == "SingletonCo", null);
|
||||
|
||||
Assert.NotNull(result);
|
||||
Assert.Same(acme, result);
|
||||
}
|
||||
finally
|
||||
{
|
||||
DbBase.Context = null; // cleanup
|
||||
}
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------
|
||||
// (14) Client.FindByName delegates to DbBase.Find and works correctly.
|
||||
// ---------------------------------------------------------------------
|
||||
@@ -268,6 +292,28 @@ public class BeforeTests
|
||||
Assert.Null(result);
|
||||
}
|
||||
|
||||
// (15b) FindByName with no context argument uses the Context singleton.
|
||||
[Fact]
|
||||
public void Client_FindByName_uses_Context_singleton_when_db_omitted()
|
||||
{
|
||||
var db = new DbContext();
|
||||
var acme = new Client { Name = "Acme Corp" };
|
||||
db.Attach(acme);
|
||||
|
||||
DbBase.Context = db;
|
||||
try
|
||||
{
|
||||
var result = Client.FindByName("Acme Corp");
|
||||
|
||||
Assert.NotNull(result);
|
||||
Assert.Same(acme, result);
|
||||
}
|
||||
finally
|
||||
{
|
||||
DbBase.Context = null; // cleanup
|
||||
}
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------
|
||||
// (16) The class-level DbBase.Context singleton can be used as the
|
||||
// implicit context source for Find operations — the classic
|
||||
|
||||
Reference in New Issue
Block a user