How apex-enterprise-patterns/force-di works
Project type and purpose
• This is a specialized Salesforce “core” library providing a generic Dependency Injection (DI) framework for Apex, Visualforce, Lightning (Aura) and Flow runtime injection.
• The README confirms that the key deliverable is an injectable Apex API (di_Injector and related classes), plus Visualforce/Lightning components (<c:di_injector>, di_injectorFlowProxy) that orchestrate runtime injection based on custom metadata and platform cache.
File structure
• Top-level folder force-di/ contains two sub-folders:
• main/ — all production metadata:
– classes/ — Apex DI engine: bindings, modules, injector controllers, platform cache integration
– aura/ — Lightning Aura components & events for UI-side injection
– components/ — Visualforce components for DI
– objects/ — Custom Metadata Types (di_Binding__mdt, di_Configurations__c) and their fields, layouts, validation rules, list views
– layouts/ — page layouts for metadata
• test/ — Apex unit tests and test suite definitions
• Presence of sfdx-project.json, scratch definitions, and GitHub CI workflows indicates an SFDX-based CI/CD deployment pipeline.
Key insights from the README
• The framework’s entry points are:
• Apex static API: di_Injector and helpers (di_Module, di_Binding, di_PlatformCache)
• UI components: <c:di_injector> for Visualforce/Lightning and di_injectorFlowProxy for Flows
• Configuration is driven by Custom Metadata (di_Binding__mdt) and optionally accelerated via Platform Cache (di_PlatformCache.cls).
Main components
a. Client Triggers (UI & Flows)
• Visualforce pages / Lightning Aura Components / Flow elements that declare <c:di_injector> or invoke the di_Injector service
b. DI Engine (Apex Classes)
• di_Injector (core controller)
• di_Module (scans/loads implementation modules)
• di_Binding (represents a binding record)
• di_BindingConfigWrapper, di_BindingParam (wraps metadata for runtime)
• di_PlatformCache (optional cache layer)
c. Configuration Store
• Custom Metadata Types (CMTs): di_Binding__mdt, di_Configurations__c
• Salesforce Platform Cache (if enabled via CMT)
d. Salesforce Metadata & Runtime Platform
• Apex runtime, Lightning runtime, Flow engine
• Events: Aura events (di_injectorAttributeChangeEvent, di_injectorFlowProxyStatusChanged)
Relationships and interactions
• Client Triggers call the DI Engine’s entry point (di_Injector) or drop in the UI Components (di_injector.cmp)
• DI Engine queries Custom Metadata (di_Binding__mdt) to resolve which concrete implementation to inject
• di_PlatformCache caches binding lookups for performance
• DI Engine instantiates the target classes (via dynamic Apex) and returns the concrete implementation to the caller
• In Lightning, the Aura components communicate via events to wire injected attributes at runtime
• Flow Proxy component bridges Flow inputs/outputs with the DI Engine
Architectural patterns and design principles
• Dependency Injection / Service Locator hybrid: centralized di_Injector that locates and returns implementations
• Configuration-driven: uses Custom Metadata for binding declarations
• Extension points: di_Module allows custom modules to register additional bindings or override behavior
• Performance optimization: optional Platform Cache layer
• Clear component names: di_Injector, di_Module, di_Binding, di_PlatformCache, <c:di_injector>, di_injectorFlowProxy