As in the previous chapter, this section provides some general design and best practice guidelines around designing a Domain layer class for a given object. Note that some of these conventions are shared by the Service layer, as it also calls the Domain layer because conventions such as bulkification apply to the logic written here as well.
The key principle of the Domain layer pattern is to lay out the code in such a way that it maps to the business domain of the application. In a Force.com application, this is typically supported by the Custom Objects. As such, it's important to clearly indicate which Domain layer class relates to which Standard or Custom Object:
Race.cls
and RaceDomain.cls
Races.cls
and Teams.cls
onEventName
convention. Other method names should be descriptive and avoid repeating the name of the class as part of the name.Races.checkInserts
and Races.startRace
Races.onBeforeInsert
and Races.start
Races.onBeforeInsert( List<Race__c>racesBeingInserted) Races.start( List<Id>raceIds)
Races.onBeforeInsert() Races.start()
The following diagram shows how the Domain classes that are used in this chapter map to their respective Custom Objects. Each Domain class receives the records that its methods act on in its constructor. The methods shown in the Domain classes are described in more detail as we progress through this chapter.
As with the Service layer, code written in the Domain layer is encouraged to think about records in a bulk context. This is important when Domain class methods are being called from an Apex Trigger context and the Service layer code, both of which carry the same bulkification requirement.
Instead of enforcing bulkification through passing parameters and ensuring that parameter types are bulkified, as with the Service layer, the information in the Domain class logic acts on and is driven by its member variables, which are bulkified. In this case, the Records
class property returns a List<SObject>
object:
public void onValidate() {
for(Contestant__c race : (List<Contestant__c>) Records) {
}
}
Most methods on Domain classes already have the information they need through the Records
property (the state the Domain class was constructed with). As such, there is little need to pass data information on the methods themselves. However, for some custom behavior methods (those not reflecting the Apex Trigger logic), you might want to pass other domain objects and/or a Unit Of Work as additional parameters. Examples later in this chapter help illustrate this.
52.14.39.59