SelectorFactory methods

The SelectorFactory class has two methods on it: newInstance and selectById. The following code illustrates both these methods by showing two equivalent usage examples to retrieve records given a set of IDs:

List<Contestant__c> contestants =  
  Application.Selector.selectById(contestantIds);  

List<Contestant__c> contestants = Application.Selector.newInstance(Contestant__c.SObjectType) .selectSObjectsById(contestantIds);

As you can see from the preceding code, the selectById method provides a shortcut to access the generic selectSObjectById method from the fflib_ISObjectSelector interface and, as saw earlier in this chapter, is implemented by the fflib_SObjectSelector base class. As such, all Selector classes are guaranteed to provide it. It offers a cleaner way of querying records in the standard way without having to know the Apex class name for the Selector. This effectively is how the Domain Factory can dynamically instantiate a Domain class instance loaded with records on behalf of the developer.

The method internally uses Id.getSObjectType (on the first ID that is passed in) to look up the Selector class through the map defined previously and instantiate it (via Type.newInstance).

You would use the newInstance method instead of just instantiating directly via the new operator in the Selector class when you are writing generic code that handles different types of objects. In other cases, you can continue to instantiate Selector classes in the normal manner using the new operator.

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

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