trigger固然好用,但是随着项目中的需求越来越多,如果都写在trigger文件中,不同的人去写在不同的地方,后续会出现代码越来越庞大,越来越乱,后续不便于维护的情况,所以基于trigger的使用,可以基于简单的框架来使用。以下内容为框架操作。

前置操作,系统中创建一个Hierarchy custom setting,用作白名单。默认值为false。

Untitled

TriggerHandlerExtension.cls:抽象类,用来其他的trigger handler继承此类,实现execute方法从而实现符合自身业务的逻辑内容。

public  with sharing abstract class TriggerHandlerExtension {

    public void execute() {
        if(Trigger.new == null && Trigger.old == null) {
            return;
        }

        Rule_Setting__c ruleSetting = Rule_Setting__c.getInstance(UserInfo.getUserId());
        if(ruleSetting.Disable_Trigger__c) {
            return;
        } 

        switch on Trigger.operationType {
            when BEFORE_INSERT  { this.beforeInsert  (Trigger.new); }
            when BEFORE_UPDATE  { this.beforeUpdate  (Trigger.old, Trigger.oldMap, Trigger.new, Trigger.newMap); }
            when BEFORE_DELETE  { this.beforeDelete  (Trigger.old, Trigger.oldMap); }
            when AFTER_INSERT   { this.afterInsert   (Trigger.new, Trigger.newMap); }
            when AFTER_UPDATE   { this.afterUpdate   (Trigger.old, Trigger.oldMap, Trigger.new, Trigger.newMap); }
            when AFTER_DELETE   { this.afterDelete   (Trigger.old, Trigger.oldMap); }
            when AFTER_UNDELETE { this.afterUndelete (Trigger.new, Trigger.newMap); }
        }

    }

    protected virtual void beforeInsert  (List<SObject> newList) {}

    protected virtual void beforeUpdate  (List<SObject> oldList, Map<Id, SObject> oldMap, List<SObject> newList, Map<Id, SObject> newMap) {}

    protected virtual void beforeDelete  (List<SObject> oldList, Map<Id, SObject> oldMap) {}

    protected virtual void afterInsert   (List<SObject> newList, Map<Id, SObject> newMap) {}

    protected virtual void afterUpdate   (List<SObject> oldList, Map<Id, SObject> oldMap, List<SObject> newList, Map<Id, SObject> newMap) {}

    protected virtual void afterDelete   (List<SObject> oldList, Map<Id, SObject> oldMap) {}

    protected virtual void afterUndelete (List<SObject> newList, Map<Id, SObject> newMap) {}

}

AccountTriggerHandler:继承 TriggerHandlerExtension类,并且实现相关的虚拟方法。实际项目中可能还要往下分层,调用 AccountBizLogic等情况,以实际项目情况为主。

public with sharing class AccountTriggerHandler  extends TriggerHandlerExtension {
    public override void beforeInsert(List<SObject> newList) {
        //TODO
        for(Account accountItem : (List<Account>)newList) {
        	if(String.isBlank(accountItem.Industry)) {
				accountItem.addError('Industry 不能为空');
			}
        }
    }

    public override void beforeUpdate(List<SObject> oldList, Map<Id, SObject> oldMap, List<SObject> newList, Map<Id, SObject> newMap) {
        //TODO
        for(Account accountItem : (List<Account>)newList) {
        	if(String.isBlank(accountItem.Industry)) {
				accountItem.addError('Industry 不能为空');
			}
        }
    }

    public override void beforeDelete(List<SObject> oldList, Map<Id, SObject> oldMap) {
        //TODO
    }

    public override void afterInsert(List<SObject> newList, Map<Id, SObject> newMap) {
        //TODO
    }

    public override void afterUpdate(List<SObject> oldList, Map<Id, SObject> oldMap, List<SObject> newList, Map<Id, SObject> newMap) {
        //TODO
    }

    public override void afterDelete(List<SObject> oldList, Map<Id, SObject> oldMap) {
        //TODO
    }
}

AccountTrigger.trigger:进行改造。直接调用 AccountTriggerHandler的execute的方法即可。

trigger AccountTrigger on Account(before insert, before update) {
    new AccountTriggerHandler().execute();
}