D'fflib
HOW

Adoption

Apply D'fflib gradually in your codebase and enable your team: a recommended layer sequence, refactoring decisions, team learning and AI-assisted development.

Installing the package itself won't change code quality. This page helps you absorb and apply the patterns to realise the benefits. Not installed yet? Start with Get started.

Application

Once D'fflib is installed, there are multiple ways to start applying (and benefiting from) the framework. When your team is fully familiar with the framework and starting a greenfield project, you can simply enforce it from day one. In most situations, however, a gradual adoption is more practical: it allows the team to get familiar without disrupting productivity or overhauling an existing codebase all at once.

Incremental application

Aside from the structuring of code, the biggest benefit of fflib comes from the ability to mock methods in unit tests and reduce deployment time significantly. To realise these benefits, prioritise moving database interactions (queries and DML) onto (D')fflib first so these features can be leveraged.

Based on experience, we recommend starting with a specific layer sequence, and tailor the guidelines/enforcement based on your projects' code and team experience. Start with minor adjustments that don't heavily depend on the framework signatures. This allows you to make things tangible, gain experience and realise benefits quickly.

Note that this is a recommendation and not the only way. The right approach differs by project and team context. Regardless of the approach, consistency is key. Once you decide on an approach, including naming conventions and standards, commit to it and make sure all developers apply it consistently.

Layer adoption sequence

Selectors
Start by moving queries to selectors as this is the simplest step, prepares for future usage and is easy to track (number of inline queries).

Domain statics
Move SObject-specific statics (like statuses or picklist values) to the domain layer to start deduplicating and centralising them. Small SObject-specific utility methods can also be gradually moved here.

Unit of Work
Move DML operations to the Unit of Work, enabling you to start mocking unit tests, avoid test data insertion and reduce deployment times. This also simplifies complex (relational) DML operations, ensures transaction management is always applied (removing repetitive duplicate code), and guarantees only one DML interaction per SObject per transaction.

Test Factory
Move (local) test data creation to Test Factories to enable reuse and centralise test data construction.

Triggers
Move each new trigger to the metadata-driven trigger handler, enabling efficient unit tests and streamlined, clear transaction management.

Entrypoints
Clearly separate backend initiations into dedicated entry layer classes (Batch, LWC/Aura/VF controllers, Flows, Queueable, etc.). Avoid any business logic: they solely own the invocation of other layers, transaction management and possible mutations/translations of payloads and texts.

Business logic to Domains and Services
Move all business logic to either Domains or Services depending on their scope, focusing on separation of concerns, deduplication and maintainability.

Code refactoring guidelines

To help set expectations and guide the adoption process, the following flowchart outlines the recommended approach based on developer familiarity and the state of the Salesforce org.


Adoption & learning

Regardless of the starting point, developers should be trained and enabled to maximise the benefits of (D')fflib, and thus ensure a sustainable and maintainable Salesforce implementation. Plan learning, support and championship consciously.

Below is a set of recommended sequential steps you can consider when planning adoption and learning of (D')fflib within the team.

Appoint Champions
It helps to designate experienced team members as champions who can pave the path, guide others, answer questions, and ensure consistent application of (D')fflib principles across the project.

Raise developer awareness
Ensure the team members understand the purpose and benefits. Understanding the high-level concepts of (D')fflib will help them to be motivated to learn and apply.

Build framework knowledge
Host training or knowledge sharing sessions to build a solid understanding of the framework among team members. Start with the basics, including its layers and core principles of (D')fflib. Followed by applying per layer.

Learning by doing
Start with small, incremental tasks to apply the framework in practice. Supported by the Champions, this hands-on experience helps reinforce learning and build confidence (and often enthusiasm) among team members.

Embrace AI support
Despite the importance of proper understanding, AI support can accelerate learning and application by providing context-aware suggestions, code snippets, and guidance aligned with (D')fflib principles. Our AI skill and LLMs.txt might help the team get up to speed faster.

Evaluate periodically
It's important to evaluate with the team on their progress and satisfaction, and adjust learning, adoption and application strategies accordingly.

Provide ongoing support
Establish channels for continuous support, such as regular Q&A sessions, documentation updates, and peer reviews, to help team members overcome challenges and reinforce best practices.

Set clear priorities with (business) stakeholders
Adopting a new framework consumes time, but it is a valuable investment for the long-term success of the project. It's crucial to, upfront, align with stakeholders on the priorities and expected benefits. This enables the team with sufficient time to learn, grow and mature.

Define realistic milestones
Together with the team, define clear milestones or objectives, both on individual level of proficiency, but also on tangible code metrics. For example, 80% of queries migrated to selectors, more code delivered in (D')fflib-structured classes than legacy code or full unit test runtimes reduced by 30%. This can help to create some motivation and purpose for the team, as success becomes tangible and measurable. Ensure to celebrate success, and also define clear guidelines when the 'investment' becomes the new standard.

Is your team stuck or facing challenges? We're happy to support, with e.g. tailored trainings or a maturity assessment that help your team take the next steps.

Frequently Asked Questions

For questions on installation, relevancy and applicability for your project, → visit the FAQ page.


Except for the listed items, the content on this site is licensed under CC-BY-NC-4.0
and allows sharing, provided an appropriate credit and link to source.

On this page