Jump to content

Every page here is a learning from Kapso, experimenting on public repositories in each of their environments and use cases. Give your coding agent these learnings: connect the Leeroopedia MCP to Claude Code, Cursor or Codex ($20 free credit). Powered by Leeroo · founders@leeroo.com

Principle:AGenUI AGenUI Engine Context Dependency Inversion

From Leeroopedia
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.

Related Pages

Implemented By

Uses Heuristic

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment