
Salesforce's Best Example of Separation of Concerns: fflib Apex Common Framework
Discover the power of the fflib Apex Common framework in Salesforce, the best example of Separation of Concerns. Learn how fflib is structured, how it works, and how to use it for clean, maintainable, and scalable code.
SALESFORCE BEST PRACTICES
8/11/20243 min read
In the world of Salesforce development, maintaining clean, modular, and scalable code is a top priority. The fflib Apex Common framework is a shining example of how the Separation of Concerns (SoC) principle can be effectively implemented in Salesforce Apex. This framework provides a structured approach to organizing your code, making it easier to manage, test, and extend. In this blog post, we’ll dive into the fflib Apex Common framework, explore its structure, understand how it works, and discuss the benefits it brings to Salesforce development.
What Is the fflib Apex Common Framework?
fflib Apex Common is a widely-used, open-source framework that provides a set of design patterns and best practices for Salesforce Apex development. It was created by FinancialForce and is built around the principles of Separation of Concerns, ensuring that your code is modular, testable, and scalable. The library can be found at: https://fflib.dev/
How Is fflib Structured?
The fflib framework is organized into several key components, each serving a specific purpose within the application architecture:
1. Domain Classes
Domain classes encapsulate the business logic related to specific Salesforce objects. They manage the behavior and rules associated with those objects, ensuring that the logic is centralized and reusable across different parts of the application.
Example:


2. Service Classes
Service classes in fflib are responsible for orchestrating business processes and coordinating interactions between different domain classes. They act as a bridge between the controller layer and the domain logic.
Example:
3. Selector Classes
Selector classes handle all data retrieval operations, encapsulating SOQL queries and ensuring that data access is separate from business logic. This makes the code more modular and easier to test.
Example:
4. Unit of Work
The Unit of Work pattern in fflib manages DML operations, ensuring that all changes are tracked and committed as a single transaction. This helps maintain data consistency and reduces the risk of errors.
How Does fflib Work?
Fflib Apex Common works by clearly defining the roles of each component in the architecture:
Domain Classes: Contain business logic specific to Salesforce objects.
Service Classes: Orchestrate business processes and interact with domain classes.
Selector Classes: Manage data retrieval and isolate SOQL queries.
Unit of Work: Tracks and commits DML operations, ensuring transaction integrity.
This structure enforces a clear separation between data access, business logic, and transaction management, making your code more modular and maintainable.
Benefits of Using fflib
1. Modular and Maintainable Code
Fflib promotes a clean separation between different layers of your application, making it easier to maintain and extend over time.
2. Improved Testability
With business logic encapsulated in domain classes and data access in selector classes, you can easily write unit tests for each component, leading to more reliable code.
3. Scalability
As your Salesforce org grows, the fflib framework ensures that your code remains scalable, allowing you to manage complexity without sacrificing performance.
4. Reusability
By following the fflib patterns, you create reusable components that can be leveraged across different parts of your application, reducing code duplication and improving consistency.
How to Use fflib
To start using fflib in your Salesforce projects:
Install the fflib Apex Common framework: You can install it directly from the GitHub repository.
Create Domain, Service, and Selector Classes: Follow the fflib patterns to structure your code.
Leverage Unit of Work: Use the Unit of Work pattern to manage complex transactions across multiple objects.
Write Tests: Take advantage of the modular structure to write comprehensive unit tests for each component.
Conclusion
The fflib Apex Common framework is the best example of implementing the Separation of Concerns principle in Salesforce development. By structuring your code with domain classes, service classes, selector classes, and the Unit of Work pattern, you create a modular, maintainable, and scalable application. Incorporate fflib into your Salesforce projects to ensure your code remains clean, testable, and ready to handle the complexities of any business process.
FAQs
1. What is the fflib Apex Common framework? fflib Apex Common is an open-source framework that provides design patterns and best practices for Salesforce Apex development, focusing on Separation of Concerns.
2. How does fflib improve code maintainability? fflib structures your code into distinct components (Domain, Service, Selector, Unit of Work) that separate data access, business logic, and transaction management, making it easier to maintain.
3. What are the main components of fflib? The main components of fflib are Domain Classes, Service Classes, Selector Classes, and the Unit of Work pattern.
4. How does fflib enhance testability in Salesforce? By isolating business logic in domain classes and data access in selector classes, fflib makes it easier to write unit tests for each part of your application.
5. Can fflib be used in existing Salesforce projects? Yes, fflib can be integrated into existing projects. Start by refactoring your code into the fflib structure to take advantage of its benefits.
