diff --git a/.gitignore b/.gitignore index 66db31d..5f0cf05 100644 --- a/.gitignore +++ b/.gitignore @@ -7,6 +7,7 @@ obj/ .vscode/ *.user *.suo +.idea ## Dotnet CLI .dotnet/ diff --git a/src/Before/DbContext.cs b/src/Before/DbContext.cs index 225ad42..3a3a9ca 100644 --- a/src/Before/DbContext.cs +++ b/src/Before/DbContext.cs @@ -18,6 +18,21 @@ public class DbContext // maintains a separate set per entity type. private readonly Dictionary> _tables = new(); + // ----------------------------------------------------------------- + // Simulated DB latency. Every "round trip" through this context — + // Attach, Save, Find, IsTracked, Tracked — sleeps for this long, the + // way a real database makes every call pay a network + query cost. + // Deliberately on by default: the console demo and the tests are + // supposed to feel sluggish, so the cost of persistence becomes + // visible. Set to TimeSpan.Zero for an instant fake. + // ----------------------------------------------------------------- + /// + /// Simulated latency per DB operation. Default 200 ms. + /// + public static TimeSpan Latency { get; set; } = TimeSpan.FromMilliseconds(200); + + private static void SimulateLatency() => Thread.Sleep(Latency); + /// /// Get or create the per-type table for . /// This mirrors EF's / Set<T> accessor. @@ -55,6 +70,7 @@ public class DbContext /// public void Attach(DbBase entity) { + SimulateLatency(); ArgumentNullException.ThrowIfNull(entity); entity.DbContext = this; var table = TableFor(entity.GetType()); @@ -70,6 +86,7 @@ public class DbContext /// public int Save() { + SimulateLatency(); var saved = 0; foreach (var table in _tables.Values) foreach (var entity in table) @@ -95,6 +112,7 @@ public class DbContext /// The first matching entity, or null when no match. public T? Find(Predicate predicate) where T : DbBase { + SimulateLatency(); var table = Table(); foreach (var e in table) if (e is T candidate && predicate(candidate)) @@ -108,6 +126,7 @@ public class DbContext /// public bool IsTracked(DbBase entity) { + SimulateLatency(); foreach (var table in _tables.Values) if (table.Contains(entity)) return true; @@ -122,6 +141,7 @@ public class DbContext { get { + SimulateLatency(); var all = new List(); foreach (var table in _tables.Values) all.AddRange(table);