Selector Method Injection
AT4DX keeps one selector class per SObject while allowing downstream packages to add queries that depend on their own schema. For Account, every package uses AccountsSelector; there is no SalesAccountsSelector or MarketingAccountsSelector.
Use a normal selector method when the query is shared by every consumer:
List<Account> accounts = AccountsSelector.newInstance().selectById(accountIds);
Use selector method injection when the query belongs to a consuming package:
List<Account> accounts = AccountsSelector.newInstance().selectInjection(
SelectBySloganSelectorMethod.class,
params
);
Why It Exists
Suppose Slogan__c is supplied by sales-marketing-common, but only Marketing needs to query by slogan. Adding selectBySlogan to Org Common's AccountsSelector would force Org Common to depend upward on that schema. Creating a second Account selector would violate the one-selector-per-SObject rule and duplicate common behavior.
Instead, Marketing owns a small injectable query class. AT4DX gives that class the query factory created by AccountsSelector, including the selector's standard fields and all fields supplied through selector field injection.
sequenceDiagram
participant M as Marketing service
participant A as AccountsSelector
participant I as SelectBySloganSelectorMethod
participant D as Database
M->>A: newInstance().selectInjection(class, params)
A->>I: class.newInstance()
A->>I: setParameters(params)
A->>I: setQueryFactory(newQueryFactory())
I->>I: selectQuery()
I->>D: Database.query(queryFactory.toSOQL())
D-->>M: List<Account>
There is no selector-method binding metadata. The caller supplies the injectable Apex Type explicitly. ApplicationSObjectSelector.selectInjection(...) constructs it, supplies the parameters and query factory, then calls selectQuery().
List Query Example
This is the pattern used by SelectBySloganSelectorMethod in at4dx-samplecode:
// Package: marketing-app
public inherited sharing class SelectBySloganSelectorMethod
extends AbstractSelectorMethodInjectable
implements ISelectorMethodInjectable {
public List<SObject> selectQuery() {
Parameters params = (Parameters) getParameters();
Set<String> slogans = params.slogans;
return Database.query(
newQueryFactory()
.setCondition(Account.Slogan__c + ' IN :slogans')
.toSOQL()
);
}
public class Parameters implements ISelectorMethodParameterable {
public Set<String> slogans;
}
}
Call it inline through the singular Account selector:
SelectBySloganSelectorMethod.Parameters params =
new SelectBySloganSelectorMethod.Parameters();
params.slogans = new Set<String>{ 'Customer first', 'Built to last' };
List<Account> accounts = AccountsSelector.newInstance().selectInjection(
SelectBySloganSelectorMethod.class,
params
);
An injectable reads its typed parameters with getParameters() and builds its query from newQueryFactory().
QueryLocator Example
Batch Apex can use the parallel QueryLocator API:
public inherited sharing class SelectStrategicAccountsQueryLocatorMethod
extends AbstractSelectorQueryLocatorInjectable
implements ISelectorQueryLocatorMethodInjectable {
public Database.QueryLocator selectQueryLocator() {
Parameters params = (Parameters) getParameters();
Decimal minimumRevenue = params.minimumRevenue;
return Database.getQueryLocator(
newQueryFactory()
.setCondition(Account.AnnualRevenue + ' >= :minimumRevenue')
.toSOQL()
);
}
public class Parameters implements ISelectorMethodParameterable {
public Decimal minimumRevenue;
}
}
SelectStrategicAccountsQueryLocatorMethod.Parameters params =
new SelectStrategicAccountsQueryLocatorMethod.Parameters();
params.minimumRevenue = 1000000;
Database.QueryLocator locator =
AccountsSelector.newInstance().selectQueryLocatorInjection(
SelectStrategicAccountsQueryLocatorMethod.class,
params
);
Parameters
Parameter objects cross the generic selector boundary, so they implement ISelectorMethodParameterable. Keep them focused on query inputs:
- Use Sets for
INfilters. - Normalize null or empty inputs before calling when an empty filter needs special semantics.
- Do not place DML, service calls, or business actions in a selector method.
- Use bind variables rather than concatenating untrusted values into SOQL.
Pass null only for a genuinely parameterless injectable. AT4DX does not call setParameters(...) when the argument is null.
Package Placement
| Artifact | Package |
|---|---|
IAccountsSelector, AccountsSelector, selector binding |
org-common |
Shared Slogan__c schema |
sales-marketing-common |
SelectBySloganSelectorMethod used only by Marketing |
marketing-app |
| Marketing service call site | marketing-app |
If both Sales and Marketing need the identical query contract, the injectable can move into sales-marketing-common. The singular AccountsSelector remains in Org Common either way.
Field Injection Still Applies
Selector method injection adds a WHERE, ordering, grouping, or QueryLocator behavior. It does not replace field injection. newQueryFactory() begins with the Account selector's common fields and incorporates fieldsets registered through SelectorConfig_FieldSetInclusion__mdt.
Use:
- Selector Field Injection to add downstream fields to selector results;
- Selector Method Injection to add downstream query behavior.
Testing
Test the injectable directly by supplying a real query factory only when you need to verify its generated query or database behavior. At a service boundary, mock IAccountsSelector and stub the inherited selectInjection(...) call:
IAccountsSelector selector =
(IAccountsSelector) mocks.mock(IAccountsSelector.class);
mocks.startStubbing();
// Required: Application.Selector.setMock() asks the mock for its SObjectType.
mocks.when(selector.sObjectType()).thenReturn(Account.SObjectType);
mocks.when(selector.selectInjection(
SelectBySloganSelectorMethod.class,
params
)).thenReturn(expectedAccounts);
mocks.stopStubbing();
Application.Selector.setMock(Account.SObjectType, selector);
Production code still uses the inline call:
List<Account> accounts = AccountsSelector.newInstance().selectInjection(
SelectBySloganSelectorMethod.class,
params
);
Common Errors
| Error | Cause | Fix |
|---|---|---|
Type does not implement ISelectorMethodInjectable |
A list query class implements the wrong interface | Extend AbstractSelectorMethodInjectable and implement ISelectorMethodInjectable |
Type does not implement ISelectorQueryLocatorMethodInjectable |
A QueryLocator call received a list injectable | Extend AbstractSelectorQueryLocatorInjectable and implement the QueryLocator interface |
| Null parameter cast | selectQuery() expects parameters but the caller passed null |
Construct and pass the injectable's Parameters object |
| Custom field unavailable at compile time | The injectable package lacks the schema dependency | Put genuinely shared schema in the correctly scoped Business Common package and declare that dependency |
| Duplicate Account selector | App-specific query behavior was implemented through subclassing | Remove the alternate selector and move the query into an injectable method |
Next Steps
- Domain Process Injection applies the same extension principle to domain behavior.
- Multi-App – Single Org Example combines common selector methods, method injection, Criteria, Actions, and Unit of Work.