Bulkification

It's well known that the best practice of Apex is to implement bulkification within Apex Triggers, mainly because they can receive and process many records of the same type. This is also true for the use of StandardSetController classes or Batch Apex, for example.

As we identified in the previous chapter, handling bulk sets of records is a common requirement. In fact, it's one that exists throughout your code paths, since DML or SOQL in a loop at any level in your code will risk hitting governor limits.

For this reason, when designing methods on the Service layer, it is appropriate that you consider list parameters by default. This encourages the development of bulkified code within the method and avoids the caller having to call the Service method in a loop.

The following is an example of non-bulkified code:

RaceService.awardChampionShipPoints(Id raceId)

The following is an example of bulkified code:

RaceService.awardChampionShipPoints(Set<Id>raceIds) 

A non-bulkified method with several parameters might look like this, for example:

RaceService.retireFromRace(Id raceId, String reason) 

A bulkified version of the preceding method signature can utilize an Apex inner class, as described earlier and shown in the following example, in the Defining and passing data subsection.

Sometimes, implementing bulkified versions of Service methods are more costly and complex than what the callers will realistically require, so this should not be seen as a fixed guideline, but should at least always be considered.
..................Content has been hidden....................

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