mradisis / apex-dependency-injection

Apex Dependency Injection Sample

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Apex Dependency Injection

This repository demonstrates Apex Dependency Injection with a sample shipping service that allows to generate tracking numbers.

Configuration

The sample shipping service provides two mock implementations: FedEx and DHL.
The service implementations are configured in a Service_Implementations__mdt custom metadata. The Shipping__c key of the custom metadata must map to a comma-delimited list of implementations such as DHL,FedEx.
The injector uses that list to load the service implementation classes with an Impl suffix (DHLImpl and FedExImpl).

Usage

The service can be accessed via the ShippingInjector class in two ways:

Option 1: Using the default implementation

// Get the first implementation configured in Service_Implementations__c custom metadata
ShippingService service = ShippingInjector.getDefaultService(); 
// Assuming that the default implementaiton is DHL
String trackingNumber = service.generateTrackingNumber(); // => DHL-XXXX

Option 2: Using a specific implementation

// Get the FedEx implementation
ShippingService service = ShippingInjector.getService('FedEx');
String trackingNumber = service.generateTrackingNumber(); // => FEX-XXXX

Check out the ShippingTest test class for more details on how to use the service.

Installation

Create a scratch org:
sfdx force:org:create -a pattern -s -f config/project-scratch-def.json

Push source to scratch org:
sfdx force:source:push

Run Apex tests and get code coverage:
sfdx force:apex:test:run -c -r human -w 10

About

Apex Dependency Injection Sample

License:Creative Commons Zero v1.0 Universal


Languages

Language:Apex 94.5%Language:Shell 5.5%