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

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

2025-06-19 06:32:31作者:秋阔奎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提供的平衡性和灵活性使其成为首选。

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

项目优选

收起
openGauss-serveropenGauss-server
openGauss kernel ~ openGauss is an open source relational database management system
C++
136
187
RuoYi-Vue3RuoYi-Vue3
🎉 (RuoYi)官方仓库 基于SpringBoot,Spring Security,JWT,Vue3 & Vite、Element Plus 的前后端分离权限管理系统
Vue
880
520
openHiTLSopenHiTLS
旨在打造算法先进、性能卓越、高效敏捷、安全可靠的密码套件,通过轻量级、可剪裁的软件技术架构满足各行业不同场景的多样化要求,让密码技术应用更简单,同时探索后量子等先进算法创新实践,构建密码前沿技术底座!
C
361
381
ohos_react_nativeohos_react_native
React Native鸿蒙化仓库
C++
181
264
kernelkernel
deepin linux kernel
C
22
5
nop-entropynop-entropy
Nop Platform 2.0是基于可逆计算理论实现的采用面向语言编程范式的新一代低代码开发平台,包含基于全新原理从零开始研发的GraphQL引擎、ORM引擎、工作流引擎、报表引擎、规则引擎、批处理引引擎等完整设计。nop-entropy是它的后端部分,采用java语言实现,可选择集成Spring框架或者Quarkus框架。中小企业可以免费商用
Java
7
0
CangjieCommunityCangjieCommunity
为仓颉编程语言开发者打造活跃、开放、高质量的社区环境
Markdown
1.09 K
0
note-gennote-gen
一款跨平台的 Markdown AI 笔记软件,致力于使用 AI 建立记录和写作的桥梁。
TSX
83
4
cherry-studiocherry-studio
🍒 Cherry Studio 是一款支持多个 LLM 提供商的桌面客户端
TypeScript
613
60
open-eBackupopen-eBackup
open-eBackup是一款开源备份软件,采用集群高扩展架构,通过应用备份通用框架、并行备份等技术,为主流数据库、虚拟化、文件系统、大数据等应用提供E2E的数据备份、恢复等能力,帮助用户实现关键数据高效保护。
HTML
118
78