Skip to content
wimvelzeboer edited this page Feb 8, 2024 · 16 revisions

Welcome to the fflib-apex-extensions wiki!

Extensions for the fflib-apex-common Library. Introducing many advanced features on top of the Apex Enterprise Patterns

The basics of Separation of Concerns

The complete picture

+------------+ +----------------+ +------------+ +----------------+ +------------+ +-----------------------+
| Controller | | TriggerHandler | | Batch Apex | | Scheduled Apex | | WebService | | Inbound Email Handler |
+------------+ +----------------+ +------------+ +----------------+ +------------+ +-----------------------+
       |                |                |               |                |                   |
       |                |                V               V                |                   |
       |                |               +-----------------+               |                   |
       +----------------+-------------->|     Service     |<--------------+-------------------+
                                        +-----------------+
                                           ^    ^    |
                                           |    |    |
       +-----------------------------------+    |    +---------------------------------+
       |                                        |                                      |
       |           +------------+               |               +--------------+       |
       |           |   Domain   |<--------------+-------------->|   Selector   |       |
       |           +------------+                               +--------------+       |
       V                                                                               V
+---------------+                                                          +--------------------+
| Factory Class |                                                          | SObject UnitOfWork |
+---------------+                                                          +--------------------+

              +-----------+    +-----------+    +------------+    +-----------+
              | Constants |    |   Logger  |    | UnitOfWork |    | Factories |
              +-----------+    +-----------+    +------------+    +-----------+
Core Layers Entry Points Supporting Layers

Domain Layer
Selector Layer
Service Layer

Controllers
TriggerHandler
Batch Apex
Scheduled Apex
WebService
Inbound Email Handler

Constants
Factories
UnitOfWork:
- Generic UnitOfWork
- SObject UnitOfWork
Logger

Concern Core design principles

Service

  • Only contains complex business logic

  • Handles business logic that goes cross domain,
    e.g. Accounts and Contacts

Domain

  • Is a wrapper around a list of records/object

  • Contains simple business logic, which concerns only ONE domain,
    e.g. Accounts

  • Is not aware of any other domain/service other than itself, so it does not have references to other classes.

Selectors

  • Contains all SOQL queries for a certain SObjectType

  • Avoid referencing other SObjectTypes like in sub-queries or related fields

  • Avoid querying non-deterministic formula fields

Controller

  • Validates authentication to business processes

  • Validates the provided input

  • Calls business logic

    • Use Services for complex logic

    • Selectors and/or domains for simple logic

    • Keep logic to a minimum

  • Optionally: Translate/scrutinize and return data

Scheduled Apex

  • Define Cron schedule

  • Provides methods to:

    • execute

    • cancel job

  • Calls business logic from Service class

fflib-apex-extensions features

Roadmap features

  • Queued Actions / Apex Events
    A replacement for the scheduled actions of process builder flows, as it is difficult to update flows with scheduled actions. This feature contains a queue object with actions and scheduled jobs to execute the actions when they are scheduled. Events are used to tell the application that the given action should be performed.
    See the Events documentation for a full description

  • Selector Architecture
    The selector will be redesigned to that its not coupled to a database. In this new architecture the selector will retrieve data from wherever it is stored. It should not be the concern of a service class calling the selector to know where the data is coming from. This new architecture should allow for getting the data from the list below.
    The queries should also be able to roll-over. Like when the requested record is not in memory, then the next selector in line should be called as the record might be in the Platform cache. If it is still not found the record can be queried from a database.

    • In memory from previous queries (Identity Map Design Pattern)

    • A Platform Cache

    • The Database

    • External WebService

  • UUID generator

How to structure your project

Folder structure:

sfdx-source
 |
 +-- force-app or {NameSpacePrefix}
     |
     +-- main
     |   |
     |   +-- default
     |   |   |
     |   |   +-- classes
     |   |       |  Application.cls
     |   |       |
     |   |       +-- constants
     |   |       |      AccountLabels.cls
     |   |       |      AccountNames.cls
     |   |       |
     |   |       +-- controllers
     |   |       |      AccountListViewController.cls
     |   |       |
     |   |       +-- domains
     |   |       |   |  Accounts.cls
     |   |       |   |
     |   |       |   +-- interfaces
     |   |       |          IAccounts.cls
     |   |       |
     |   |       +-- factories
     |   |       |      AccountFactory.cls
     |   |       |
     |   |       +-- selectors
     |   |       |   |  AccountsSelector.cls
     |   |       |   |
     |   |       |   +-- interfaces
     |   |       |          IAccountsSelector.cls
     |   |       |
     |   |       +-- services
     |   |       |   |  AccountsServices.cls
     |   |       |   |  AccountsServicesImp.cls
     |   |       |   |
     |   |       |   +-- interfaces
     |   |       |          IAccountsServices.cls
     |   |       |
     |   |       +-- triggerActions
     |   |           |
     |   |           +-- Account
     |   |           |      TA_Account_CopyCountryToContacts.cls
     |   |           |      TA_Account_RecalculateRating.cls
     |   |           +-- Contact
     |   |           |
     |   |            ...
     |   |
     |   +-- test
     |       |
     |       +-- apex
     |           +-- classes
     |               |
     |               +-- domains
     |               |     AccountsTest.cls
     |               |
     |               +-- service
     |               |     AccountsServiceTest.cls
     |               |
     |               +-- triggerHandlers
     |               |     AccountTriggerHandlerTest.cls
     |
     +-- Feature SFX
         |
         +-- main
         |   |
         |   +-- classes
         |       |
         |       +-- domains
         |       |
         |        ...
         |
         +-- test
             |
              ...

Donations

We are pleased to announce that the fflib-apex-extensions Project is accepting donations in the privacy-oriented cryptocurrency Monero (XMR) at the following address:

8BMsQrdLiBvKaP87GFMPE9Wa1US79ygcp3b9kwoDq1iBW9JEYbcgpDS3hMp2p7ePWiCEKg2fdTucUbpNTtGbnvCCQZTMSu1

Thank you for your donations!