Application Factory Injection

AT4DX supplies one Application entry point with four factories: Selector, Domain, Service, and UnitOfWork. force-di resolves their implementations from custom metadata.

Factory Summary

Factory Call Binding metadata Result
Selector Application.Selector.newInstance(SObjectType) ApplicationFactory_SelectorBinding__mdt IApplicationSObjectSelector
Domain Application.Domain.newInstance(records) ApplicationFactory_DomainBinding__mdt IApplicationSObjectDomain
Service Application.Service.newInstance(Type) ApplicationFactory_ServiceBinding__mdt Requested service interface
UnitOfWork Application.UnitOfWork.newInstance() ApplicationFactory_UnitOfWorkBinding__mdt IApplicationSObjectUnitOfWork

Selector Factory

Register the one selector for each SObject:

ApplicationFactory_SelectorBinding__mdt
  BindingSObject__c: Account
  To__c:             AccountsSelector
  Priority__c:       10

Business applications do not register competing Sales or Marketing selectors. Fields are extended with Selector Field Injection, and query methods are extended with Selector Method Injection.

The concrete selector normally exposes a typed static wrapper:

public static IAccountsSelector newInstance() {
    return (IAccountsSelector)
        Application.Selector.newInstance(Account.SObjectType);
}

Call a common method inline:

List<Account> accounts = AccountsSelector.newInstance().selectById(accountIds);

Call a package-specific method inline through injection:

List<Account> accounts = AccountsSelector.newInstance().selectInjection(
    SelectLargeAccountsSelectorMethod.class,
    params
);

Selector Priority__c still controls force-di resolution when a legitimate implementation replacement is installed. It is not a mechanism for creating an app-specific selector per SObject.

Mocking

IAccountsSelector selector =
    (IAccountsSelector) mocks.mock(IAccountsSelector.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);

Production code continues calling AccountsSelector.newInstance(), which returns the mock during the test.

Domain Factory

Register exactly one domain constructor per SObject:

ApplicationFactory_DomainBinding__mdt
  BindingSObject__c: Account
  To__c:             AccountDomain.Constructor

To__c names the Constructor inner class, not the domain class itself.

public with sharing class AccountDomain extends ApplicationSObjectDomain {
    public AccountDomain(List<SObject> records) {
        super(records);
    }

    public class Constructor implements fflib_SObjectDomain.IConstructable {
        public fflib_SObjectDomain construct(List<SObject> records) {
            return new AccountDomain(records);
        }
    }
}

There is no SalesAccountDomain or MarketingAccountDomain. Those packages inject Criteria and Actions into AccountDomain with DomainProcessBinding__mdt.

Wrap records when application code needs the domain explicitly:

IApplicationSObjectDomain domain = Application.Domain.newInstance(accounts);

An Account trigger uses the one domain class:

trigger AccountTrigger on Account (
    before insert, before update, before delete,
    after insert, after update, after delete, after undelete
) {
    fflib_SObjectDomain.triggerHandler(AccountDomain.class);
}

ApplicationSObjectDomain automatically runs matching trigger-context domain process bindings after its base lifecycle handling. Do not add a Sales domain subclass just to invoke the coordinator.

Mocking

IApplicationSObjectDomain domain =
    (IApplicationSObjectDomain) mocks.mock(IApplicationSObjectDomain.class);
Application.Domain.setMock(domain);

Service Factory

An AT4DX service is a triad: an interface, a static entry-point class, and a registered implementation. The binding points at the implementation:

ApplicationFactory_ServiceBinding__mdt
  BindingInterface__c: ISalesAccountsService
  To__c:               SalesAccountsServiceImpl
  Priority__c:         10
// 1. Interface — the package boundary consumers compile against.
public interface ISalesAccountsService {
    void processAccounts(Set<Id> accountIds);
}

// 2. Static entry point — what callers use. Hides the factory.
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);
    }
}

// 3. Implementation — registered in the binding, never referenced by name.
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();
    }
}

Callers use the static entry point and never touch the factory:

SalesAccountsService.processAccounts(accountIds);

If Sales and Marketing both compile against this contract, place the interface in sales-marketing-common. That does not imply Service depends on that package.

Mocking

Mock the interface and register it for the implementation's binding key:

ISalesAccountsService service =
    (ISalesAccountsService) mocks.mock(ISalesAccountsService.class);
Application.Service.setMock(ISalesAccountsService.class, service);

UnitOfWork Factory

Unit of Work bindings define the supported SObjects and dependency order:

ApplicationFactory_UnitOfWorkBinding__mdt
  Account      BindingSequence__c=10
  Opportunity  BindingSequence__c=20
  Order__c     BindingSequence__c=30
  OrderItem__c BindingSequence__c=40

Use the AT4DX registration API:

IApplicationSObjectUnitOfWork uow =
    Application.UnitOfWork.newInstance();

uow.register(accounts);
uow.register(newOpportunity, Opportunity.AccountId, parentAccount);
uow.commitWork();

register(...) has single-record, list, and parent-related overloads — use it in place of fflib's registerDirty(...)/registerNew(...).

Mocking

IApplicationSObjectUnitOfWork uow =
    (IApplicationSObjectUnitOfWork)
        mocks.mock(IApplicationSObjectUnitOfWork.class);
Application.UnitOfWork.setMock(uow);

((IApplicationSObjectUnitOfWork) mocks.verify(uow, 1))
    .register(expectedAccounts);

Combined Flow

flowchart LR
    S[Sales service] --> AS[AccountsSelector.newInstance]
    AS --> SB[(Account → AccountsSelector)]
    AS --> Q{Query kind}
    Q -->|common| CM[selectById]
    Q -->|Sales-specific| IM[selectInjection]
    S --> U[Application.UnitOfWork]
    U --> UB[(SObject dependency order)]
    U --> DML[register + commitWork]
    DML --> T[Account trigger]
    T --> AD[AccountDomain]
    AD --> DP[Injected Criteria + Actions]

Application Is Framework Code

Application is shipped by AT4DX — you don't write it. You deploy binding records (the ApplicationFactory_*__mdt rows shown above) and call the four factories; force-di does the wiring. There is no per-app factory class to build or maintain.

Next Steps

→ Selector Field Injection