Add static Context (Singleton) to DbBase

ActiveRecord pattern: expose a shared DbContext on DbBase so any
entity subclass can reach it without passing context through parameters.
The instance-level DbContext back-reference still works; the static one
provides a class-level global fallback.
This commit is contained in:
2026-09-14 20:48:21 +01:00
parent c4bfc14f32
commit 8b46fccf08
2 changed files with 34 additions and 2 deletions
+15
View File
@@ -11,6 +11,21 @@ namespace Before;
/// </summary>
public class DbBase
{
// -----------------------------------------------------------------
// Static singleton: an ActiveRecord-style shared context that any
// entity can reach without passing it through method parameters.
// -----------------------------------------------------------------
/// <summary>
/// Shared (singleton) <see cref="DbContext" /> accessible from every
/// entity via its base type. Set once at application startup so that
/// entity methods can call <c>DbBase.Context!</c> instead of carrying
/// a context reference.
///
/// This is yet another leak: domain objects depend on the persistence
/// layer at the *type* level, not just the instance level.
/// </summary>
public static DbContext? Context { get; set; }
/// <summary>
/// Primary key. Fresh (unsaved) entities have <see cref="Guid.Empty"/>;
/// <see cref="DbContext.Save"/> assigns a real id, faking EF's identity