Naming conventions

By now, you're starting to get the idea about naming conventions. The Selector classes and methods borrow guidelines from other layers with a few tweaks. Consider the following naming conventions when writing the Selector code:

  • Class names: In naming a Selector class, you typically follow the same convention as a Domain class, taking the plural name of the object it is associated with and appending the word Selector at the end. However, you can group common cross-object queries into a single module scoped class, for example, InvoicingSelector. The following lists some good and bad examples of this convention:
    • Some bad examples are RaceSOQLHelper and SOQLHelper.
    • Some good examples are RacesSelector, DriversSelector, and RacingAnalyticsSelector.
  • Method names: The select prefix is a good way to express the shared purpose of the Selector methods, followed by a description of the primary criteria and/or relationships used. As with the Service and Domain methods, avoid repeating terms already used in the class name:
    • Some bad examples are getRecords, getDrivers, loadDrivers, selectDriversById, and selectRacesAndContestants.
    • Some good examples are selectById, selectByTeam, selectByIdWithContestants, and selectByIdWithContestantsAndDrivers.
  • Method signatures: Selector methods typically return a Map, List, or QueryLocator method exposing the resulting SObjects, thereby supporting the bulkification needs of the platform and other layers such as the Domain class constructors and Batch Apex. Method parameters reflect the parameterized aspects of the WHERE clause; again, these should also be bulkified whenever it is applicable to do so:
    • These examples are bad because they either do not accept lists or their method names are not descriptive enough:
selectById(Id recordId) 
DriverSelector.select(Set<Id>teamIds) 
Database.QueryLocatorqueryForBatch(Set<Id> ids)
    • These examples are good because they take lists that support bulkification:
selectById(Set<Id> raceIds) 
selectByTeam(Set<Id> teamIds) 

In this section, we covered some subtle, but important, naming conventions in how you name your methods and define the parameters they take. Paying attention to these details is important to how other developers understand and consume the features of a given selector class. In the following section, we will discuss how this applies to bulkifcaiton, which is a common best practice on the Lightning Platform.

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

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