Apex Enterprise Patterns Sample Code

repository·master·Indexed 42 days ago

https://github.com/apex-enterprise-patterns/fflib-apex-common-samplecode

A sample application demonstrating the implementation of Apex Enterprise Patterns on the Salesforce Lightning Platform. It provides examples of applying the Service, Selector, and Domain layers across Custom Buttons, Batch Apex, Integration APIs, and Apex Triggers, including the use of fflib for User Mode and CRUD/FLS enforcement.

Tokens
547
Snippets
1
Records
3
Agent score
3%

What's inside Apex Enterprise Patterns

  1. How the Sample Application implements Enterprise Patterns

    master

    The sample application demonstrates how to apply Apex Enterprise Patterns across different Salesforce platform features:

    • Custom Buttons: Implements UI logic in Controllers that call the Service Layer.
    • Batch Apex: Demonstrates reusing Service and Selector Layer code within a Batch context.
    • Integration API: Shows how to expose an Integration API via the Service Layer using Apex and REST.
    • Apex Triggers: Demonstrates factoring trigger logic through the Domain Layer (using wrappers).
  2. Implement User Mode and CRUD/FLS

    master

    The sample application enforces Field-Level Security (FLS) using fflib user mode. To implement this pattern in your own code, follow these approaches used in the sample:

    Selectors

    In selector constructors, pass fflib_SObjectSelector.DataAccess.USER_MODE to the super call to ensure queries respect FLS.

    // Example pattern from sample
    super(false, fflib_SObjectSelector.DataAccess.USER_MODE);

    Unit of Work

    Use a UserModeUnitOfWorkFactory (typically defined in your Application.cls) that utilizes UserModeDML() to ensure DML operations respect FLS.

    Testing User Mode

    To test code running in user mode:

    1. Use TestDataFactory to create a Standard User.
    2. Assign the ApexEnterprisePatternsSampleApp permission set to that user to grant necessary field-level access.
    3. Wrap your test execution in System.runAs(getRunAsUser()).

    Note: Data setup requiring elevated permissions (e.g., inserting PricebookEntry) should still run in system context.

    // Selector Constructor Pattern
    public class MySelector extends fflib_SObjectSelector {
        public MySelector() {
            super(false, fflib_SObjectSelector.DataAccess.USER_MODE);
        }
    }