grzmol/vibe-force/sf-fflib-foundations
Claude Code and agent skill by grzmol/vibe-force
- community
Checking that the SKILL.md file is still in the repository.
Covers the fflib Apex Enterprise Patterns stack (fflib-apex-common, fflib-apex-mocks) - the four-layer Service/Domain/Selector/Unit of Work separation, the Application factory class with its SObject-to-class binding maps, interface plus implementation naming, installation by vendoring or git submodule, sfdx-project.json and .forceignore wiring for a dedicated fflib package directory, the setMock injection points used by unit tests, PMD/Code Analyzer ruleset scoping for fflib code, and an honest decision table for when plain trigger handlers plus service classes are the better answer. Use it when a project adds or removes fflib, when creating or editing Application.cls, when deciding whether a requirement deserves a Service/Domain/Selector split, when migrating legacy Apex to Apex Enterprise Patterns, or when fflib_Application.DeveloperException, "No implementation registered for service interface", or "Selector class not found for SObjectType" appears in a log.
Install
npx vskill@latest install grzmol/vibe-force/sf-fflib-foundationsSKILL.md
The same SKILL.md was published earlier as grzmol/vibe-force/sf-fflib-foundations.
fflib Foundations: Apex Enterprise Patterns
Source of truth for every signature below: apex-enterprise-patterns/fflib-apex-common @ master commit dab5977 (sfdx-source/apex-common/main/classes/*.cls), fflib-apex-mocks @ master commit d81e9e1, fflib-apex-common-samplecode @ master commit 4657d63. Anything not read from those files this session is marked [unverified].
When to use
| Situation | Use this skill | |
|---|---|---|
| Adding fflib to a repo for the first time | Yes - references/install-and-layout.md | |
Writing or fixing Application.cls | Yes - references/application-factory.md | |
| Choosing fflib vs trigger handler + service class | Yes - references/fflib-vs-plain-apex.md | |
Looking up what an fflib_* class does | Yes - references/class-catalogue.md | |
| Writing a selector or a query | Skill sf-fflib-selector-layer | |
| Writing a domain, service, or Unit of Work | Skill sf-fflib-domain-service-uow | |
| Mocking, stubbing, verifying with ApexMocks | Skill sf-fflib-testing | |
| Trigger wiring, batch entry points, day-2 operations | Skill sf-fflib-operations | |
| Plain Apex idioms, no fflib in the repo | Skill sf-apex-development |
Decision table: does fflib earn its keep here?
| Signal | fflib | Plain trigger handler + service classes | |
|---|---|---|---|
| SObjects with non-trivial write logic | > 5 | <= 5 | |
| Apex classes in the package | > 40 | < 40 | |
| Multiple entry points per SObject (trigger, LWC, REST, batch, Flow invocable) | Yes | One or two | |
Unit tests must run without DML (Set<Id> + stubs) | Required | Nice to have | |
| Second-generation packaging with a shared base library | Yes | No | |
| Team has used Apex Enterprise Patterns before | Yes | No, and no budget to learn | |
| Automation mostly Flow / Record-Triggered Flow | No | Yes - see skill sf-flow-automation | |
| Org is a single-admin org with < 10 Apex classes | No | Yes |
Two or more "plain" answers means do not adopt fflib. The library costs roughly 3 extra files per SObject (interface, implementation, mock registration) and a permanent onboarding tax. See references/fflib-vs-plain-apex.md for the same requirement implemented both ways.
Core patterns
1. The four layers and the dependency rules
Enforceable rules, in priority order:
| Rule | Why | |
|---|---|---|
Only the Service layer calls commitWork() | one transaction boundary per use case | |
| Selectors never call services or domains | they would become untestable and recursive | |
| Domains never issue SOQL or DML directly | they get records in, register work out | |
| Entry points contain no business logic, only marshalling | lets Flow/LWC/REST share behaviour | |
Every layer is reached through Application.*, never new Impl() in product code | mock injection | |
Service methods take and return bulk shapes (Set<Id>, List<SObject>) | see skill sf-governor-limits |
2. Application.cls is the only wiring file
One class per package, four static finals. Verified shape from fflib-apex-common-samplecode/.../main/classes/Application.cls:
newInstance resolves the registered Type and calls Type.newInstance(), so every registered class needs a public zero-argument constructor. Unregistered lookups throw fflib_Application.DeveloperException with No implementation registered for service interface X or Selector class not found for SObjectType X (fflib_Application.cls). Full binding tables, the UserModeDML factory subclass and the common wiring mistakes are in references/application-factory.md.
3. Naming and file layout
The samplecode conventions, verified against its class paths:
| Layer | Interface | Implementation | Caller writes | |
|---|---|---|---|---|
| Service | IAccountsService | AccountsServiceImpl | AccountsService.updateOpportunityActivity(ids) (static facade) | |
| Domain | IAccounts | Accounts (+ inner Constructor) | (IAccounts) Application.Domain.newInstance(ids) | |
| Selector | IAccountsSelector extends fflib_ISObjectSelector | AccountsSelector | AccountsSelector.newInstance() | |
| Unit of Work | fflib_ISObjectUnitOfWork | fflib_SObjectUnitOfWork | Application.UnitOfWork.newInstance() |
The service layer is three files: a with sharing static facade (AccountsService), the interface (IAccountsService), and the implementation (AccountsServiceImpl). The facade exists so callers keep static call syntax while the factory still controls instantiation:
Directory layout inside a package directory (samplecode uses exactly these folder names):
4. Installation: vendor, submodule, or deploy
fflib-apex-common @ master publishes no unlocked-package version id and no GitHub release; the repo has zero releases and its README advertises only a GitHub-deploy button. Any 04t... install id for these libraries is [unverified] - do not put one in a script.
fflib-apex-common's current README states "Dependencies: None for deployment or for running this library's Apex tests"; its docs/changelog.md still carries the September 2014 note that ApexMocks "must be deployed to the org before deploying this library". The README reflects master - deploy mocks first anyway, it is free and removes the ambiguity. Submodule and upgrade procedures: references/install-and-layout.md. Packaging consequences: skills sf-project-structure and sf-packaging-release.
5. Mock injection points
setMock on every factory is protected and @TestVisible, so it compiles only from test context (fflib_Application.cls).
| Call | Argument type | |
|---|---|---|
Application.UnitOfWork.setMock(uowMock) | fflib_ISObjectUnitOfWork | |
Application.Service.setMock(IAccountsService.class, serviceMock) | Type, Object | |
Application.Selector.setMock(selectorMock) | fflib_ISObjectSelector - requires sObjectType() stubbed | |
Application.Selector.setMock(Account.SObjectType, selectorMock) | SObjectType, fflib_ISObjectSelector | |
Application.Domain.setMock(domainMock) | fflib_ISObjectDomain (uses sObjectType()) or fflib_IDomain (uses getType()) | |
Application.Domain.setMock(Account.SObjectType, domainMock) | SObjectType, fflib_ISObjectDomain |
The single-argument selector and domain overloads call sObjectType() on the mock; if it is not stubbed the mock returns null and registration silently binds to null. fflib logs an INFO System.debug in that path. Prefer the two-argument overload. Detail: skill sf-fflib-testing.
6. Incremental migration from legacy Apex (strangler)
| Step | Action | Old code | |
|---|---|---|---|
| 1 | Deploy fflib-apex-mocks + fflib-apex-common, add empty Application.cls | untouched | |
| 2 | Extract queries of the busiest SObject into XSelector, register it | legacy class calls XSelector.newInstance().selectById(ids) | |
| 3 | Introduce fflib_ISObjectUnitOfWork in the one method with the most DML statements | DML removed from legacy class | |
| 4 | Create IXService/XServiceImpl, move the legacy method body in verbatim | legacy public method becomes a one-line delegator | |
| 5 | Introduce the Domain class only once two callers need the same record-level rule | - | |
| 6 | Convert the trigger last, after Service and Selector exist | - |
Keep the legacy class as a thin delegator until every caller is migrated, then delete it in the same release; do not leave two ways to do the same thing. Never mix a legacy [SELECT ...] and a selector for the same SObject in one class.
7. Static analysis and performance characteristics
fflib's own source suppresses PMD rules (fflib_SObjectSelector.cls header suppresses PMD.CyclomaticComplexity, PMD.ExcessivePublicCount, PMD.ApexDoc and more), so the library deploys clean only if your analyzer run excludes vendored paths. In your own fflib code expect:
| Rule | Trigger | Handling | |
|---|---|---|---|
ApexCRUDViolation | Database.query(qf.toSOQL()) - PMD cannot see the WITH USER_MODE that toSOQL() emits | configure readAuthMethodPattern for your selector base, or scope the rule out of selectors/ | |
ApexSOQLInjection | dynamic query string built by fflib_QueryFactory | same; use Database.queryWithBinds for user input | |
ApexSharingViolations | AccountsServiceImpl without a sharing keyword | always declare with sharing or inherited sharing | |
ApexDoc, ApexUnitTestClassShouldHaveAsserts | generated interfaces, Impl classes | keep as advisory, not gating |
This is the start of the file. Read the full SKILL.md on GitHub.
Security
- Security scanPassed, 95 of 100Pattern checks on the skill's files.
- Model reviewPassed
Quality evaluation
- Task evaluationNot runNo evaluation yet. Run one in Skill Studio.
Source
- SKILL.md in the repositoryCheckingskills/sf-fflib-foundations/SKILL.md
Popularity
- Repository stars
- 3
- 7-day trend score
- 7
Stars and forks are for the whole GitHub repository, not this skill alone. Not refreshed yet.
Works with
Any agent vskill installs to: 56 targets in all. These 39 have a page on this site.
- Claude Code
- Cursor
- GitHub Copilot
- Windsurf
- Codex
- Gemini CLI
- Cline
- Amp
- Roo Code
- Goose
- Continue
- Kilo Code
- Junie
- Trae
- OpenHands
- Qwen Code
- AI Agent
- Tabnine Chat
- Sourcegraph Cody
- Amazon Q
- Firebase Genkit
- Sweep
- Devin
- Phind
- Mentat
- Claude Desktop
- Aider
- GPT Pilot
- Plandex
- bolt.new
- v0
- Replit Agent
- Anse
- TypingMind
- BetterChatGPT
- LibreChat
- Warp AI
- Pieces
- Fig AI
Embed badge
[](https://verified-skill.com/skills/grzmol/vibe-force/sf-fflib-foundations)