# apex-enterprise-patterns/force-di architecture

> Architecture diagram of the GitHub repository apex-enterprise-patterns/force-di, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-04-28). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/apex-enterprise-patterns/force-di
- Repository: https://github.com/apex-enterprise-patterns/force-di

## Overview

##### 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`

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Client Layer
    subgraph "Client Triggers (UI & Flows)" 
        direction TB
        VF["<c:di_injector> (Visualforce)"]:::uiComponents
        FlowProxyVF["di_injectorFlowProxy (Visualforce Flow)"]:::uiComponents
        AuraInjector["di_injector.cmp (Aura Injector)"]:::uiComponents
        AuraAttribute["di_injectorAttribute.cmp (Aura Attribute)"]:::uiComponents
        AuraFlowProxy["di_injectorFlowProxy.cmp (Aura Flow Proxy)"]:::uiComponents
        AuraEventAttr["di_injectorAttributeChangeEvent.evt"]:::uiComponents
        AuraEventFlow["di_injectorFlowProxyStatusChanged.evt"]:::uiComponents
    end

    %% DI Engine Layer
    subgraph "DI Engine (Apex)" 
        direction TB
        Injector["di_Injector.cls"]:::apexEngine
        ModuleLoader["di_Module.cls"]:::apexEngine
        BindingModel["di_Binding.cls"]:::apexEngine
        BindingWrapper["di_BindingConfigWrapper.cls"]:::apexEngine
        BindingParam["di_BindingParam.cls"]:::apexEngine
        PlatformCache["di_PlatformCache.cls"]:::apexEngine
        CompController["di_InjectorComponentController.cls"]:::apexEngine
        FlowProxyCtrl["di_InjectorComponentFlowProxyController.cls"]:::apexEngine
        GenericCtrl["di_InjectorController.cls"]:::apexEngine
        FlowHelper["di_Flow.cls"]:::apexEngine
    end

    %% Configuration Store
    subgraph "Configuration Store" 
        direction TB
        CMDT["di_Binding__mdt"]:::config
        ConfigObj["di_Configurations__c"]:::config
        Layouts["di_Binding__mdt-Binding Layout"]:::config
        CacheDB["Platform Cache"]:::config
    end

    %% Data Flows
    VF -->|"invoke()"| Injector
    FlowProxyVF -->|"invokeFlow()"| FlowProxyCtrl
    AuraInjector -->|"invoke()"| CompController
    AuraFlowProxy -->|"invokeFlow()"| FlowProxyCtrl
    Injector -->|"loadModules()"| ModuleLoader
    ModuleLoader -->|"resolveBindings()"| BindingModel
    BindingModel -->|"wrap()"| BindingWrapper
    BindingWrapper -->|"params()"| BindingParam
    Injector -->|"cacheGet()/cacheSet()"| PlatformCache
    Injector -->|"queryBindings()"| CMDT
    Injector -->|"queryConfigs()"| ConfigObj
    Injector -->|"instantiate()"| GenericCtrl
    GenericCtrl -->|"executeFlow()"| FlowHelper

    %% UI Events
    AuraInjector -->|"fireAttributeChange"| AuraEventAttr
    AuraAttribute -->|"listen"| AuraEventAttr
    AuraFlowProxy -->|"fireStatusChanged"| AuraEventFlow
    AuraFlowProxy -->|"listen"| AuraEventFlow

    %% Config to Cache
    CMDT -->|"optionally uses"| CacheDB

    %% Return to Client
    GenericCtrl -->|"returnInstance()"| VF
    GenericCtrl -->|"returnInstance()"| AuraInjector
    FlowHelper -->|"returnFlowResult()"| FlowProxyVF
    FlowHelper -->|"returnFlowResult()"| AuraFlowProxy

    %% Click Events
    click VF "https://github.com/apex-enterprise-patterns/force-di/blob/master/force-di/main/components/di_injector.component"
    click FlowProxyVF "https://github.com/apex-enterprise-patterns/force-di/blob/master/force-di/main/components/di_injectorFlowProxy.component"
    click AuraInjector "https://github.com/apex-enterprise-patterns/force-di/blob/master/force-di/main/aura/di_injector/di_injector.cmp"
    click AuraAttribute "https://github.com/apex-enterprise-patterns/force-di/blob/master/force-di/main/aura/di_injectorAttribute/di_injectorAttribute.cmp"
    click AuraEventAttr "https://github.com/apex-enterprise-patterns/force-di/blob/master/force-di/main/aura/di_injectorAttributeChangeEvent/di_injectorAttributeChangeEvent.evt"
    click AuraFlowProxy "https://github.com/apex-enterprise-patterns/force-di/blob/master/force-di/main/aura/di_injectorFlowProxy/di_injectorFlowProxy.cmp"
    click AuraEventFlow "https://github.com/apex-enterprise-patterns/force-di/blob/master/force-di/main/aura/di_injectorFlowProxyStatusChanged/di_injectorFlowProxyStatusChanged.evt"
    click Injector "https://github.com/apex-enterprise-patterns/force-di/blob/master/force-di/main/classes/di_Injector.cls"
    click ModuleLoader "https://github.com/apex-enterprise-patterns/force-di/blob/master/force-di/main/classes/di_Module.cls"
    click BindingModel "https://github.com/apex-enterprise-patterns/force-di/blob/master/force-di/main/classes/di_Binding.cls"
    click BindingWrapper "https://github.com/apex-enterprise-patterns/force-di/blob/master/force-di/main/classes/di_BindingConfigWrapper.cls"
    click BindingParam "https://github.com/apex-enterprise-patterns/force-di/blob/master/force-di/main/classes/di_BindingParam.cls"
    click PlatformCache "https://github.com/apex-enterprise-patterns/force-di/blob/master/force-di/main/classes/di_PlatformCache.cls"
    click FlowProxyCtrl "https://github.com/apex-enterprise-patterns/force-di/blob/master/force-di/main/classes/di_InjectorComponentFlowProxyController.cls"
    click CompController "https://github.com/apex-enterprise-patterns/force-di/blob/master/force-di/main/classes/di_InjectorComponentController.cls"
    click GenericCtrl "https://github.com/apex-enterprise-patterns/force-di/blob/master/force-di/main/classes/di_InjectorController.cls"
    click FlowHelper "https://github.com/apex-enterprise-patterns/force-di/blob/master/force-di/main/classes/di_Flow.cls"
    click CMDT "https://github.com/apex-enterprise-patterns/force-di/tree/master/force-di/main/objects/di_Binding__mdt/"
    click ConfigObj "https://github.com/apex-enterprise-patterns/force-di/tree/master/force-di/main/objects/di_Configurations__c/"
    click Layouts "https://github.com/apex-enterprise-patterns/force-di/blob/master/force-di/main/layouts/di_Binding__mdt-Binding Layout.layout-meta.xml"

    %% Styles
    classDef apexEngine fill:#cfe2f3,stroke:#000,stroke-width:1px
    classDef uiComponents fill:#d9ead3,stroke:#000,stroke-width:1px
    classDef config fill:#fce5cd,stroke:#000,stroke-width:1px
    classDef database fill:#ffe599,stroke:#000,stroke-width:1px
```
