Combining Apex data types with SObject types

It is possible to combine Apex data types with SObject data types in a response from Selector methods. This is as long as when you return SObject data types, they are populated according to the fields indicated by the corresponding Selector.

For example, let's say we want to return an entire Driver__c record associated with the Contestant object, instead of just the Driver name. The change to the Summary Apex class will be to expose the queried Driver__c record, which is safe as it has been populated in accordance with DriversSelector. The following code shows how the Summary class can be modified to expose the Driver__c record, as queried in the custom Selector method:

public class Summary { 
  public Driver__c Driver {   
   get { return contestant.Driver__r; } } 
...
}

In this example, a DriversSelector instance is used to obtain the field list, which is injected into the query factory along with other specific fields:

fflib_QueryFactory contestantQueryFactory = 
newQueryFactory(false). selectField(Contestant__c.RacePosition__c). selectField('Race__r.Name'). selectField('Race__r.Season__r.Name'). selectField('Car__r.Name'). setCondition('Race__c in :raceIds'); new DriversSelector(). configureQueryFactoryFields( contestantQueryFactory, Contestant__c.Driver__c.getDescribe().getRelationshipName()); Map<Id, List<Summary>> summariesByRaceId = new Map<Id, List<Summary>>(); for(Contestant__c contestant : Database.query(contestantQueryFactory.toSOQL())) {

The preceding code shows how you can combine Selector classes to reuse the fields defined by the DriversSelector class when performing a query for Contestants in the ContestantSelector class. This ensures that records from the Driver object are returned consistently regardless of whether they are queried directly or indirectly by querying Contestants

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.142.40.32