The following diagram illustrates the flow of execution from the Apex Trigger to the Domain class, triggerHandler, and then to the individual methods:
To help you understand how traditional Apex Trigger events are mapped to the various virtual methods in the base class, the following table describes the code path taken from when the base class's fflib_SObjectDOmai.triggerHandler method is invoked, through to the specific event-based methods that can be overridden by a Domain class:
Trigger context |
Base class records property value |
Base class handle method called (refer to the following note) |
Base class event method(s) called (refer to the following note) |
Trigger.isBefore |
|||
Trigger.isInsert |
Trigger.new |
handleBeforeInsert() |
onApplyDefaults() onBeforeInsert() |
Trigger.isUpdate |
Trigger.new |
handleBeforeUpdate( |
onBeforeUpdate( |
Trigger.isDelete |
Trigger.oldMap |
handleBeforeDelete() |
onBeforeDelete() |
Trigger.isAfter |
|||
Trigger.isInsert |
Trigger.new |
handleAfterInsert() |
onValidate() onAfterInsert() |
Trigger.isUpdate |
Trigger.new |
handleAfterUpdate( |
onValidate( onAfterUpdate( |
Trigger.isDelete |
Trigger.oldMap |
handleAfterDelete() |
onAfterDelete() |
Note that handle methods can also be overridden if you wish to implement your own handling. The existingRecords parameter represents the value of Trigger.oldMap.
The base class uses the template method pattern (http://en.wikipedia.org/wiki/Template_method_pattern). This ensures that when overriding the onXXX methods from the base class, you do not need to worry about calling the base class version of the method, as is the typical developer requirement when overriding methods in OOP.