Overview
fflib is a popular open-sourced Apex enterprise level framework built for scalability, modularity and maintainability.
The fflib framework consists of several modules:
- fflib-apex-common (opens in a new tab) - Common Apex Library supporting Apex Enterprise Patterns
- fflib-apex-mocks (opens in a new tab) - An Apex mocking framework for true unit testing in Salesforce with Stub API support
- force-di (opens in a new tab) - Generic DI library with support for Apex, Triggers, Visualforce and Lightning
- at4dx (opens in a new tab) - Advanced Techniques for Salesforce DX Adoption Framework
Design principals
Usage of fflib encourages Separation of Concerns, DRY, SOLID and Dependency Injection principals.
fflib consists of four main parts.
The selector layer is home to all queries for a given object. This encourages reuse of queries and builds a central place for adjusting the fields used from the object.
The domain layer is home to sObject manipulation. The domain class also handles the trigger methods. The aim is to keep logic deduplicated and central.
The service layer is home to the business logic within the application. Majority of code will live here. The service layer will interact with Domain and Selector classes, as well as other Service classes as needed.
The implementation layer is home to a wide array of ways you can call apex across the Salesforce platform. The most basic implementation layer is an Apex Controller. A Controller is home to methods that expose the Service layers methods and business logic. Controllers may interact with multiple Service classes but should hold only very minimal logic.
Implementation layer also includes BatchApex, Scheduled Apex, APEX REST API and Flows (via invokable apex).