首页
/ SOQL-Lib项目:构建高效选择器的六种设计模式

SOQL-Lib项目:构建高效选择器的六种设计模式

2025-06-19 02:40:38作者:秋阔奎Evelyn

前言

在Salesforce开发中,SOQL查询是数据访问层的核心。SOQL-Lib项目提供了一套优雅的解决方案,帮助开发者构建灵活、可维护的选择器(Selector)模式。本文将深入解析六种不同的选择器实现方式,帮助开发者根据项目需求选择最适合的方案。

选择器模式概述

选择器模式是Salesforce开发中常用的设计模式,它将数据访问逻辑集中管理,提供以下优势:

  • 提高代码复用性
  • 统一管理字段级安全(FLS)
  • 集中控制共享规则
  • 简化复杂查询构建

方案A:继承+接口+静态方法(推荐)

这是最灵活的推荐方案,结合了继承和接口的优势:

public inherited sharing class SOQL_Account extends SOQL implements SOQL.Selector {
    public static SOQL_Account query() {
        return new SOQL_Account();
    }

    private SOQL_Account() {
        super(Account.SObjectType);
        // 默认配置
        with(Account.Id, Account.Name, Account.Type)
            .systemMode()
            .withoutSharing();
    }

    public SOQL_Account byIndustry(String industry) {
        with(Account.Industry)
            .whereAre(Filter.with(Account.Industry).equal(industry));
        return this;
    }
}

特点

  1. 使用私有构造函数确保通过静态方法创建实例
  2. 默认配置集中管理
  3. 方法链式调用提供流畅接口
  4. 业务逻辑中可灵活扩展查询条件

使用示例

List<Account> accounts = SOQL_Account.query()
    .byIndustry('IT')
    .with(Account.BillingCity)
    .toList();

方案B:组合+接口+静态方法

这种方案采用组合而非继承,通过静态方法提供查询入口:

public inherited sharing class SOQL_Contact implements SOQL.Selector {
    public static SOQL query() {
        return SOQL.of(Contact.SObjectType)
            .with(Contact.Id, Contact.Name)
            .systemMode();
    }

    public static SOQL byAccountId(Id accountId) {
        return query().whereAre(Filter.with(Contact.AccountId).equal(accountId));
    }
}

适用场景

  • 需要更轻量级的实现
  • 不希望使用继承
  • 查询逻辑相对简单

方案C:继承+非静态方法

传统面向对象风格,适合熟悉经典OOP的团队:

public inherited sharing class SOQL_Account extends SOQL {
    public SOQL_Account() {
        super(Account.SObjectType);
        with(Account.Id, Account.Name);
    }

    public SOQL_Account byIndustry(String industry) {
        return (SOQL_Account)with(Account.Industry)
            .whereAre(Filter.with(Account.Industry).equal(industry));
    }
}

特点

  • 实例方法更符合传统OOP思维
  • 需要显式类型转换保持链式调用

方案D:组合+接口+非静态方法(多团队适用)

当不同团队需要不同查询配置时,这种方案特别有用:

public inherited sharing virtual class BaseAccountSelector implements SOQL.Selector {
    public virtual SOQL query() {
        return SOQL.of(Account.SObjectType).with(Account.Id);
    }
}

public with sharing class TeamA_AccountSelector extends BaseAccountSelector {
    public override SOQL query() {
        return super.query().with(Account.AccountNumber).systemMode();
    }
}

优势

  • 基础查询可复用
  • 各团队可自定义扩展
  • 符合开闭原则

方案E:完全自定义

完全自由的设计方式,适合有特殊需求的场景:

public inherited sharing class CustomAccountSelector {
    public static SOQL getBasicQuery() {
        return SOQL.of(Account.SObjectType).with(Account.Id);
    }
}

方案F:FFLib风格

借鉴了流行的FFLib设计模式:

public inherited sharing class SOQL_Opportunity extends SOQL {
    public List<Opportunity> byAccountId(Id accountId) {
        return with(Opportunity.AccountId)
            .whereAre(Filter.with(Opportunity.AccountId).equal(accountId))
            .toList();
    }
}

最佳实践建议

  1. 安全性:始终考虑inherited sharing或显式共享模式
  2. 默认字段:在构造函数中设置最常用的字段
  3. 方法设计:保持方法单一职责,便于组合
  4. 性能:避免在循环中构建查询
  5. 可测试性:设计可mock的接口

总结

SOQL-Lib提供了多种灵活的选择器实现方式,从推荐的标准方案到完全自定义的自由模式。开发者应根据项目规模、团队习惯和具体需求选择最适合的方案。对于大多数项目,方案A提供的平衡性和灵活性使其成为首选。

登录后查看全文
热门项目推荐