Principle:AGenUI AGenUI Engine Context Dependency Inversion
| Knowledge Sources | |
|---|---|
| Domains | Software_Architecture |
| Last Updated | 2026-10-24 17:21 GMT |
Overview
A dependency inversion pattern that allows lower-level modules to access engine-level shared services through a minimal abstract interface, without depending on the concrete engine class.
Description
The Dependency Inversion Principle (the D in SOLID) states that high-level modules should not depend on low-level modules; both should depend on abstractions. In a layered engine architecture, the engine class owns and manages subsystems (template registry, style parsers, function call manager) but those subsystems also need access to engine-level services (path config, property registration). This creates a potential circular dependency: the engine includes the subsystem, and the subsystem includes the engine.
The IEngineContext interface solves this by defining a minimal read-only accessor surface. The engine implements it; subsystems depend on the interface, not the concrete engine. A global accessor function (`getEngineContext()`) provides the current context, returning `nullptr` when the engine is not running.
Interface segregation ensures only the minimal getter set is exposed: subsystems cannot accidentally call engine lifecycle methods or mutate engine state.
Usage
Use this principle when:
- Lower-level modules need access to shared engine services but should not depend on the concrete engine class
- A circular dependency between engine and subsystems must be broken
- Engine-registered metadata (e.g. deep-parse properties) needs to be queried by the parsing layer without knowing about the engine
Theoretical Basis
Without DIP (circular dependency):
AGenUIEngine → includes → TemplateRegistry
TemplateRegistry → includes → AGenUIEngine (circular!)
With DIP (interface seam):
AGenUIEngine implements IEngineContext
AGenUIEngine → includes → TemplateRegistry
TemplateRegistry → includes → IEngineContext (abstract, no cycle)
Global accessor:
getEngineContext() → IEngineContext* (nullptr if engine stopped)
Practical Guide
- Implement `IEngineContext` in the engine class; set it via `setEngineContext(this)` during `start()` and `setEngineContext(nullptr)` during `stop()`.
- Lower-level modules call `getEngineContext()` and handle `nullptr` gracefully (engine not yet started or already stopped).
- Only expose getters for services that lower-level modules actually need; do not add lifecycle or mutation methods.