D'fflib
WHATFeatures

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.

Highlights & takeaways

Upon install, D'fflib is fully metadata-driven, eliminating the risks of code conflicts and enabling the ability to structure your metadata in packages/folders. An anonymous apex script is provided to ease the migration.

Metadata-driven benefits

Packaging enabled

Layer definitions can be organised in their corresponding package, making it possible to untangle your codebase into manageable and independent packages.

Auto-inclusion of Application class

Quite often we heard teams struggle with apex-common-install due to ambiguity on the need to manually create an Application class (see below). Being metadata-driven, there is no need for projects to create and maintain the class, allowing the dfflib.cls to be auto-included upon install and ready to read from metadata.

Reduced conflicts

Due to elimination of the central Application class, there is a significant reduction of deployment conflicts and undesired rollbacks of other developers' changes during merges, as each developer will simply create a separate metadata-record instead of modifying the same class upon introduction of new classes.

Reduced dependency-compilation-errors

One compilation failure could break a large portion of your code base due to the compilation-dependency chain, as well as a longer compile-time. Moving to metadata eliminates these direct class dependencies via the central Application class.

On-demand construction

Over time we've come across many project-specific metadata-driven implementations. One clear distinction and lesson learned is to only process the requested metadata records and SObjectTypes, to avoid metadata-compilation errors when e.g. a user has no access to an object like Knowledge, or when specific objects became unavailable due to licence toggling (like Shield or FieldService).

D'fflib only retrieves and constructs the SObjectTypes at hand, preventing full-org compilation errors.

Configuration flexibility

While not recommended, configurations can be changed without code changes, like the SObject Type sequence in Unit of Work, or the implementation and enablement of layers.

Application classes compared

Below is a comparison between the new metadata-driven dfflib class, which is automatically included upon D'fflib install, and an original Application class, which had to be manually created for the original apex-common to define the layer definitions and implementations.

public with sharing class dfflib{
    public static final dfflib_Application.UnitOfWorkFactory unitOfWork = new dfflib_Application.UnitOfWorkFactory();
    public static final dfflib_Application.SelectorFactory selector = new dfflib_Application.SelectorFactory();
    public static final dfflib_Application.DomainFactory domain = new dfflib_Application.DomainFactory( selector );
    public static final dfflib_Application.ServiceFactory service = new dfflib_Application.ServiceFactory();
}
public class Application{
	// Configure and create the UnitOfWorkFactory for this Application
	public static final fflib_Application.UnitOfWorkFactory UnitOfWork =
		new fflib_Application.UnitOfWorkFactory(
            new List<SObjectType>{
                Account.SObjectType,
                Opportunity.SObjectType,
                Product2.SObjectType,
                PricebookEntry.SObjectType,
                OpportunityLineItem.SObjectType
            }
        );

	// Configure and create the ServiceFactory for this Application
	public static final fflib_Application.ServiceFactory Service =
		new fflib_Application.ServiceFactory(
            new Map<Type, Type>{
                IAccountsService.class => AccountsService.class,
            }
        );

	// Configure and create the SelectorFactory for this Application
	public static final fflib_Application.SelectorFactory Selector =
		new fflib_Application.SelectorFactory(
            new Map<SObjectType, Type>{
                Account.SObjectType     => AccountsSelector.class,
                Opportunity.SObjectType => OpportunitiesSelector.class,
                User.sObjectType        => UsersSelector.class
            }
        );

	// Configure and create the DomainFactory for this Application
	public static final fflib_Application.DomainFactory Domain =
		new fflib_Application.DomainFactory(
			Application.Selector,
			new Map<SObjectType, Type>{
                Account.SObjectType     => Accounts.Constructor.class,
                Opportunity.SObjectType => Opportunities.Constructor.class
            }
        );
}

Variable name case correction

Note the slight change in variable names, correcting from pascal to camel case.

Metadata types

D'fflib brings five metadata types to define the layer implementations and configurations. Click on the cards below to read more about the related layer.

Trigger Handler metadata adoption in progress

We're currently finalising the migration for Trigger Handler registrations from Domain class to metadata, further enabling packaging, but also a standardised trigger handler disablement via metadata.

Frequently asked questions

This is fully described in our migration guide, including an Anonymous Apex script which enables metadata record creation based on the existing Application class.

This isn't possible (yet). At initiation the metadata selection is solely based on SObjectType. Hence, the SObjectType fields have been made unique in metadata, so only one metadata record per SObjectType can exist for Domains and one for Selectors, regardless of the package it is stored in.

We've considered refactoring, but in our view that would impact backward compatibility for how Domains and Selectors are initiated. Of course we'd love to hear your thoughts and challenges on this as we do think this is (one of the) next steps for D'fflib: allowing package-specific implementation for SObject specific layers.

This is mostly dependent on your projects' package structure. Most of our projects have around 80% of the SObject specific layers (Domain, Selector) stored in the business-process-specific packages. The remaining cross-package layers are either moved to a central/core package or kept in the 'remaining' (unpackaged) force-app folder.


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