Brewtiful Beans Learns AT4DX, Part 2: The Hidden Dependency
Last time, Joe figured out how Sales, Marketing, and Service could share one AccountsSelector without stepping on each other’s toes. Fieldset inclusion and selector method injection let each team get what it needs without modifying Org Common.
Joe’s feeling pretty good about himself. He’s sipping a victory latte when he opens up the Sales service and notices something:
public with sharing class SalesAccountsService {
public static void reviewPipeline(Set<Id> accountIds) {
IAccountsSelector selector = new AccountsSelector();
List<Account> accounts = selector.selectById(accountIds);
// ...
}
}
Looks perfectly reasonable, right? He’s even using an interface!
Well, sort of.
After this post, you can:
- Explain why
new AccountsSelector()creates a dependency even when the variable is typed to an interface. - Understand how AT4DX factories separate requesting a dependency from constructing it.
- Recognize the four AT4DX factories and what each provides.
The Dependency Hiding in Plain Sight
Take another look at this line:
IAccountsSelector selector = new AccountsSelector();
The left side says, “I need something that behaves like an IAccountsSelector.”
The right side says, “And I insist on constructing this particular AccountsSelector myself.”
That’s our hidden dependency.
The service doesn’t just know what it needs. It also knows how to build it. And when Joe wants to write a focused unit test, that becomes a problem.
What if he wants his test to use a mock selector that returns a handful of fake Accounts instead of running a real SOQL query?
Tough beans. The service is hardcoded to construct the real selector. It can’t use a mock supplied through AT4DX’s selector factory.
Fortunately, Joe doesn’t have to rewrite the service or create another selector. He just needs to stop making his own coffee.
Let the Barista Handle It
Imagine walking into Brewtiful Beans and ordering a latte.
You don’t walk behind the counter, choose the espresso machine, grind the beans, and steam the milk yourself. You tell the barista what you want, and the barista figures out how to make it.
That’s essentially what a factory does.
Rather than having the service construct its own selector, Joe changes one line:
IAccountsSelector selector = AccountsSelector.newInstance();
Now the service says, “I need an Account selector. Someone else can figure out which implementation to give me.”
That someone is the AT4DX Application factory.
Behind the scenes, the shared selector exposes a little helper method:
public static IAccountsSelector newInstance() {
return (IAccountsSelector)
Application.Selector.newInstance(Account.SObjectType);
}
The call to Application.Selector looks up the configured implementation and returns it through the IAccountsSelector interface.
In production, Joe gets the Org Common AccountsSelector, just as before.
But in a unit test, he can tell the factory to serve something different:
Application.Selector.setMock(Account.SObjectType, mockSelector);
Now the same AccountsSelector.newInstance() call returns his mock.
Same service code. Same interface. Different implementation.
It’s like telling the barista, “For this order, use decaf.” The customer doesn’t need to learn how to operate a different espresso machine.
That’s the payoff: the service cares about what the selector can do, not how it’s constructed.
Four Factories, One Counter
AT4DX doesn’t stop at selectors. Remember the four fflib roles from Part 0? AT4DX provides a factory for each one.
Think of Application as the counter where your Apex code places its orders.
| Factory | What you’re ordering |
|---|---|
Application.Selector |
Something to retrieve records |
Application.Domain |
Something to enforce record-level business rules |
Application.Service |
Something to execute business operations |
Application.UnitOfWork |
Something to coordinate database changes |
Each has its own way of placing an order:
// Get the configured selector for Account
Application.Selector.newInstance(Account.SObjectType);
// Get the configured domain for these records
Application.Domain.newInstance(records);
// Get the configured service for an interface
Application.Service.newInstance(ISalesAccountsService.class);
// Get the configured Unit of Work
Application.UnitOfWork.newInstance();
The factory figures out which concrete class to return using configuration stored in Custom Metadata binding records.
That means if Joe eventually replaces an implementation, he can update its binding without editing every caller that uses the factory, provided the replacement honors the same interface.
And tests can supply mocks without changing production code.
We’ll look at exactly how those binding records work in Part 4. For now, think of them as the barista’s recipe book: they tell the factory how to fulfill each order.
One Selector Is Still One Selector
There’s an important distinction worth making.
Just because a factory can return different implementations doesn’t mean Sales, Marketing, and Service should each have their own AccountsSelector.
Remember Part 1: Org Common owns the shared selector. Applications customize it through fieldset inclusion and selector method injection.
The factory solves a different problem.
Injection lets applications extend what the selector does. The factory controls how the selector is obtained.
These two ideas work together to keep our packages loosely coupled and our code testable.
Joe gets his shared selector, the teams get their specialized queries, and nobody needs to sneak behind the counter to make their own latte.
Check Your Brew
1. What’s wrong with this line?
IAccountsSelector selector = new AccountsSelector();
- A. Apex doesn’t allow assigning classes to interfaces.
- B. It hardcodes the concrete implementation despite using an interface.
- C. Constructing a selector is slower than using a factory.
- D. It automatically bypasses field-level security.
Reveal the answer
B. The variable is typed to an interface, but the service still chooses and constructs the concrete class. Using the factory removes that construction decision from the caller.
2. What happens when a test registers a mock with Application.Selector.setMock(...)?
- A. Every selector in the org becomes a mock.
- B. The selector factory can return the registered mock when resolving that SObject.
- C. Salesforce automatically generates test Account records.
- D. The production selector class is permanently replaced.
Reveal the answer
B. The factory can return the mock for the specified SObject during the test, without requiring changes to the service code.
3. What’s the difference between selector method injection and the selector factory?
- A. They’re two names for the same feature.
- B. Method injection adds query behavior; the factory resolves which selector implementation to use.
- C. Method injection is only for tests; factories are only for production.
- D. Factories replace the need for shared selectors.
Reveal the answer
B. Injection extends the shared selector’s capabilities. The factory provides the configured selector implementation. They solve different problems.
Next up: Part 3 — The Bean Hierarchy. We’ve got shared code, application-specific code, and factories connecting everything. But where should all these classes actually live? We’ll map out the package hierarchy and build a simple decision tree for deciding where new code belongs.