FFLib Apex Common Sample

April 13, 2026 ยท View on GitHub

Push Source and Run Apex Tests

Dependencies: Must deploy Apex Mocks and Apex Common before deploying this library

LibraryDeploy
Apex MocksDeploy to Salesforce
Apex CommonDeploy to Salesforce
Apex Common Sample CodeDeploy to Salesforce

Sample Application

This repository contains a sample application illustrating the Apex Enterprise Patterns library. The aim is to illustrate a fully working sample application that demonstrates the patterns.

NOTE: The supporting Apex Common library can be found here.

Platform FeaturePatterns Used
Custom ButtonsBuilding UI logic and calling Service Layer code from Controllers
Batch ApexReusing Service and Selector Layer code from within a Batch context
Integration APIExposing an Integration API via Service Layer using Apex and REST
Apex TriggersFactoring your Apex Trigger logic via the Domain Layer (wrappers)

User Mode and CRUD/FLS

This sample enforces field-level security (FLS) by using fflib user mode throughout. Selectors pass fflib_SObjectSelector.DataAccess.USER_MODE into the super(...) constructor for FLS on queries; the UnitOfWork uses UserModeDML() via an inner UserModeUnitOfWorkFactory in Application.cls for FLS on DML. Tests that exercise USER_MODE code use TestDataFactory to create a Standard User with the ApexEnterprisePatternsSampleApp permission set, then run under System.runAs(getRunAsUser()); data setup requiring elevated permissions (e.g. PricebookEntry insert) runs in system context.

AreaApproach
Selectorssuper(false, fflib_SObjectSelector.DataAccess.USER_MODE) in selector constructors (and includeFieldSetFields overload where present)
UnitOfWorkInner UserModeUnitOfWorkFactory in Application.cls; uses UserModeDML()
TestsTestDataFactory; @TestSetup + System.runAs(getRunAsUser()) for DML/selector tests
Permission setApexEnterprisePatternsSampleApp grants field-level access for USER_MODE tests

Application Enterprise Patterns on Salesforce Lightning Platform

Design patterns are an invaluable tool for developers and architects looking to build enterprise solutions. Here are presented some tried and tested enterprise application engineering patterns that have been used in other platforms and languages. We will discuss and illustrate how patterns such as Data Mapper, Service Layer, Unit of Work and of course Model View Controller can be applied to Force.com. Applying these patterns can help manage governed resources (such as DML) better, encourage better separation-of-concerns in your logic and enforce Force.com coding best practices.

More Information on Trailhead

There are two Trailhead Modules for Apex Enterprise Patterns: