Brewtiful Beans Learns AT4DX, Part 4: The Four Factories
Joe is ready to put the Sales team’s Account review into the shared org. The service needs Accounts, it needs to save changes, and Service may eventually need to call the same business operation. He has learned to ask the factories for those collaborators instead of constructing them himself.
Then he reaches the deployment checklist: how does a factory know which class to hand back? newInstance() is a neat order at the counter, but someone still has to put the right recipe in the book.
That is the job of the binding records. Joe walks through the four factories to make sure each order can be filled—and that an Account is saved before an Opportunity that depends on it.
After this post, you can:
- Name the binding metadata type and key fields for each of the four factories.
- Explain the service triad: interface, static entry point, and implementation.
- Register Unit of Work bindings in dependency order.
First, Find the Accounts
For the shared Account selector, Joe adds a binding in ApplicationFactory_SelectorBinding__mdt:
ApplicationFactory_SelectorBinding__mdt
BindingSObject__c: Account
To__c: AccountsSelector
Priority__c: 10
Notice there’s one row per SObject. Sales doesn’t register a second Account selector, and neither does Marketing. Team-specific fields and queries are added through injection (Parts 5 and 6). Priority__c exists for a legitimate replacement of the implementation, not as a way to create a per-team selector.
The wrapper gives callers a typed entry point:
public static IAccountsSelector newInstance() {
return (IAccountsSelector)
Application.Selector.newInstance(Account.SObjectType);
}
Common methods get called inline:
List<Account> accounts = AccountsSelector.newInstance().selectById(accountIds);
Tests can substitute a selector through the factory; Part 9 looks at the mock in context.
Give the Records Their Rules
The domain binding is simpler. For Account, Joe registers the domain constructor:
ApplicationFactory_DomainBinding__mdt
BindingSObject__c: Account
To__c: AccountDomain.Constructor
To__c points at the inner Constructor class, not at AccountDomain itself. The domain class looks like this:
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);
}
}
}
The Account trigger delegates to the shared domain. Business packages supply domain rules through bindings, as we’ll see in Part 7. Code can also obtain a domain explicitly with Application.Domain.newInstance(accounts).
Let Service Call Sales
Service needs access to Sales’ operation without depending on the Sales implementation. Joe gives the teams a shared contract. The service has three parts:
- Interface. The package boundary. Consumers compile against this.
- Static entry point. What callers use. It hides the factory.
- Implementation. Registered in the binding. Nothing else names it.
Here’s the triad:
// 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();
}
}
The binding ties the interface to the implementation:
ApplicationFactory_ServiceBinding__mdt
BindingInterface__c: ISalesAccountsService
To__c: SalesAccountsServiceImpl
Priority__c: 10
Callers never see the implementation. They call the entry point:
SalesAccountsService.processAccounts(accountIds);
If you need to replace the service in a test, mock the interface and register the mock under the interface type:
ISalesAccountsService service =
(ISalesAccountsService) mocks.mock(ISalesAccountsService.class);
Application.Service.setMock(ISalesAccountsService.class, service);
Service calls this same contract, so the interface belongs in sales-service-common. Service never depends on sales-app, where the implementation lives. Part 3 covered why.
Typing Tax: Each service is three files plus a binding record. For a service that does one thing, that’s a lot of ceremony. The triad earns its keep when several teams call the same service.
Save Things in the Right Order
Joe’s service may save an Opportunity alongside its Account. If the child is saved before its new parent, the relationship cannot be set correctly. Unit of Work bindings list the SObjects it knows about and the order they commit in:
ApplicationFactory_UnitOfWorkBinding__mdt
Account BindingSequence__c=10
Opportunity BindingSequence__c=20
Order__c BindingSequence__c=30
OrderItem__c BindingSequence__c=40
Parents go first. Accounts commit before Opportunities that reference them, and Opportunities commit before the orders that reference those. Sequence numbers are whole numbers here, and the lower number commits first.
Usage follows the registration API:
IApplicationSObjectUnitOfWork uow = Application.UnitOfWork.newInstance();
uow.register(accounts);
uow.register(newOpportunity, Opportunity.AccountId, parentAccount);
uow.commitWork();
register(...) covers single records, lists, and the parent-related form shown above. Use it in place of fflib’s registerDirty(...) and registerNew(...).
A test can substitute a mock Unit of Work and verify what the service registered; Part 9 shows why that is useful.
The factories solve construction. The bindings tell them what to construct, while callers work through contracts.
Check Your Brew
1. How many Account selector bindings should exist in the org?
- A. One per team
- B. One, for Account, in
ApplicationFactory_SelectorBinding__mdt - C. One per package that queries Accounts
- D. Zero; the selector is discovered automatically
Reveal the answer
B. Each SObject has one selector binding. Team-specific behavior goes through field and method injection, not a second selector.
2. In the service triad, what does ApplicationFactory_ServiceBinding__mdt point at?
- A. The static entry-point class
- B. The trigger
- C. The Unit of Work
- D. The interface, through
BindingInterface__c, and the implementation, throughTo__c
Reveal the answer
D. The binding maps BindingInterface__c to the implementation in To__c. Callers never name the implementation directly.
3. Why does ApplicationFactory_UnitOfWorkBinding__mdt carry a BindingSequence__c?
- A. It sets the order SObjects commit in, so parents save before children
- B. It controls which field is displayed first in Setup
- C. It controls query order for the selector
- D. It limits how many records a transaction can process
Reveal the answer
A. The sequence is the commit order. Accounts (10) commit before Opportunities (20), which commit before Orders (30).
Next up: Part 5, Adding Fields Without Stirring the Pot, shows how one team adds fields to a shared selector without touching the class.