Unit of Work 2.0
Being 14x faster than the original construct, the Unit of Work 2.0 fully focuses on relaxing developers from any DML related activity.
In our perspective the essence of the Unit of Work is to centralise and aggregate DML operations, ensure proper (error) transaction management - without duplicate and scattered try-catches and rollbacks - and simplify and improve readability of complex relationship-registrations. In line with this purpose, we've redesigned the UoW to strengthen these benefits while significantly improving performance and relaxing developers in all their DML related activities.
How to use Unit of Work
A step-by-step introduction on how to leverage and master the Unit of Work, with concrete example snippets to get started.
Visit class in repository
Have a look yourself on dfflib_SObjectUnitOfWork in our repo to explore the code, methods and comments.
Main enrichments to the Unit of Work
Support for most DML operations, DMLOptions & SaveResults
Most Database methods are now supported, eliminating the need for custom IDoWork classes. Simplifying
not only the usage and mocking, but also improving readability. See full list of supported DML operations below,
including a.o. Priority, Immediate, Advanced Relationships, Approvals, Emails and (Standard) Events.
Additionally, SaveResults are (optionally) available for when a developer needs to set DMLOptions or AllOrNone = false.
Full Database Context support
See the Database Context article for standardised way to achieve full access control (sharing and permissions).
Our structure eliminates the need for project-specific overrides and 'awareness' of IDML outside of Unit of Work.
14x faster Unit of Work cycle times
Redesign of Map-initiations and numerous other optimisations
resulted in a significant performance improvement. Across multiple projects we've benchmarked >14x faster operations
.
Improved relationship registrations
- Relationships can be registered per DML Operation to prevent resolving relations too soon.
- A record instance can be registered as both new and dirty within the same Unit of Work.
- Added validation to ensure all relationships are 'executed' (e.g. register relationship, but not the child record).
Deprecation of on...-events
In our perspective these methods only caused overhead and complexity, while we never needed these in any project and believe they are in conflict with the modularity principle where one doesn't need to know how a called method performs.
Minor tweaks and fixes
- Increased test coverage (68% > 98%) after migrating to project-independent SObjectTypes.
- Added registration validations to prevent invalid combinations (e.g. publishing a non-Event).
- Allowing reuse of the same UoW instance, by resetting the work after
commitWork(). - Squashed nasty bugs, like
registerDirtyoverriding previous registrations of the same record when nodirtyFieldsare provided.
Full methods list
Below is the full method comparison between apex-common and D'fflib's dfflib_ISObjectUnitOfWork,
indicating the differences and similarities.
| Method signature | apex-common | D'fflib |
|---|---|---|
commitWork() | ||
hasWork() | ||
Constructors
| ||
setContext(dfflib_Database.Context) | ||
Register New/Insert
| ||
Register Dirty/Update
| ||
Register Upsert (deprecated)
| ||
Register Upsert
| ||
Register Delete/Recyclebin
| ||
Register Undelete
| ||
Register Custom Platform Event ( | ||
Register Work
| ||
Register Relationships
| ||
Register Approval Process
| ||
Register Email
| ||
Register Immediate
| ||
Register Priority(example)
| ||
SaveResults(example)
| ||
Reset Unit of Work
|
Metadata driven
D'fflib is fully metadata-driven, meaning all layer definitions and configurations are stored in metadata files. Originally these definitions/mappings were hardcoded in Apex classes, which increased the risk for conflicts.
Selectors 2.0
Providing full SOQL support, while unlocking proper unit test methods for the selector layer.