Dependency Injection Fundamentals
Dependency injection separates requesting a capability from constructing its implementation. AT4DX applies that separation through force-di, custom metadata, and the framework-supplied Application factories.
Hardcoded Construction
This service constructs its collaborators and configuration directly:
public class SalesAccountsServiceImpl {
public void processAccounts(Set<Id> accountIds) {
AccountsSelector selector = new AccountsSelector();
List<Account> accounts = selector.selectById(accountIds);
update accounts;
}
}
The problems are construction coupling, test substitution, and duplicated runtime configuration. The problem is not that Sales has a declared dependency on org-common; that is an intentional downward package dependency. The problem is constructing the selector directly instead of resolving it through the configured factory.
AT4DX Resolution
The same logic, written as the implementation behind a service interface, delegates construction and Unit of Work configuration:
public with sharing class SalesAccountsServiceImpl implements ISalesAccountsService {
public void processAccounts(Set<Id> accountIds) {
List<Account> accounts =
AccountsSelector.newInstance().selectById(accountIds);
IApplicationSObjectUnitOfWork uow =
Application.UnitOfWork.newInstance();
uow.register(accounts);
uow.commitWork();
}
}
AccountsSelector.newInstance() is a typed convenience wrapper around Application.Selector.newInstance(Account.SObjectType); it does not call the Apex constructor. Tests can replace the factory result with Application.Selector.setMock(...). The register(...) argument is a List<Account> — no cast to List<SObject> is needed.
This implementation is reached through the AT4DX service triad, described under Service below.
The Four Factory Boundaries
Selector
public static IAccountsSelector newInstance() {
return (IAccountsSelector)
Application.Selector.newInstance(Account.SObjectType);
}
Each SObject has one selector binding. Common methods are declared on that selector:
List<Account> accounts = AccountsSelector.newInstance().selectById(accountIds);
Application-specific methods are supplied with a selector method injection class — not another selector:
List<Account> accounts = AccountsSelector.newInstance().selectInjection(
SelectLargeAccountsSelectorMethod.class,
params
);
Domain
IApplicationSObjectDomain domain = Application.Domain.newInstance(accounts);
Each SObject has one domain binding, such as Account → AccountDomain.Constructor. Sales, Marketing, and Service add domain behavior with Criteria and Action injection, not domain subclasses.
Service
AT4DX services come as a triad: an interface, a static entry-point class, and a registered implementation. Callers use the static class and never touch the factory:
SalesAccountsService.processAccounts(accountIds);
The static class resolves the implementation through the interface:
public with sharing class SalesAccountsService {
private static ISalesAccountsService service() {
return (ISalesAccountsService)
Application.Service.newInstance(ISalesAccountsService.class);
}
public static void processAccounts(Set<Id> accountIds) {
service().processAccounts(accountIds);
}
}
ApplicationFactory_ServiceBinding__mdt binds ISalesAccountsService → SalesAccountsServiceImpl. Consumers compile only against the interface; if Sales and Marketing share it, the interface belongs in sales-marketing-common, and Marketing does not depend on the Sales implementation package.
Unit of Work
IApplicationSObjectUnitOfWork uow =
Application.UnitOfWork.newInstance();
uow.register(accounts);
uow.commitWork();
ApplicationFactory_UnitOfWorkBinding__mdt supplies the supported SObjects and insertion order. AT4DX code uses register(...), including its list and parent-related overloads.
Other Injection Mechanisms
Beyond the four factories, AT4DX injects behavior and data into the one selector and the one domain per SObject, so that if (sales) / if (marketing) branching never accumulates in Org Common. Each mechanism has its own page:
- Downstream fields on selector queries → Selector Field Injection
- Package-specific selector queries → Selector Method Injection
- Package-specific domain rules → Domain Process Injection
Metadata Resolution
flowchart LR
C[AccountsSelector.newInstance] --> F[Application.Selector factory]
F --> I[force-di]
I --> B[(SelectorBinding: Account → AccountsSelector)]
B --> S[IAccountsSelector instance]
S --> M{Method}
M -->|common| D[selectById]
M -->|package-specific| X[selectInjection]
The related metadata types are:
ApplicationFactory_SelectorBinding__mdtApplicationFactory_DomainBinding__mdtApplicationFactory_ServiceBinding__mdtApplicationFactory_UnitOfWorkBinding__mdt
Custom metadata deploys with packages, so the runtime wiring is source-controlled.
Testing Substitution
IAccountsSelector selector =
(IAccountsSelector) mocks.mock(IAccountsSelector.class);
IApplicationSObjectUnitOfWork uow =
(IApplicationSObjectUnitOfWork)
mocks.mock(IApplicationSObjectUnitOfWork.class);
// Required: Application.Selector.setMock() asks the mock for its SObjectType.
mocks.startStubbing();
mocks.when(selector.sObjectType()).thenReturn(Account.SObjectType);
mocks.stopStubbing();
Application.Selector.setMock(Account.SObjectType, selector);
Application.UnitOfWork.setMock(uow);
The production method still uses AccountsSelector.newInstance() and Application.UnitOfWork.newInstance(). Factory mocks replace the resolved instances without changing production constructors.