Dependency Injection in Salesforce Apex Class

In Salesforce, when we develop an Apex app or any Apex Class, we ensure the code is clean and maintainable to avoid difficulties during testing.

However, testing and debugging can be complex when your code depends on other classes or services.

In this scenario, we utilize dependency injection in Salesforce Apex code. This technique of structuring the code allows Apex classes to receive their dependencies from an external source rather than creating them internally.

In this Salesforce tutorial, we will learn about Dependency Injection in Salesforce and how to use it in Apex classes.

What is Dependency Injection in Salesforce?

In Salesforce, dependency injection is an Apex code pattern used to decouple code components, making them more testable and maintainable.

By injecting dependencies into classes rather than coding them, we can make Salesforce Apex classes more accessible to test, especially in unit tests.

An Apex code that is structured using Dependency Injection involves three main components:

  • Client: The class that depends on an external object (dependency).
  • Service: The dependency class that the client needs to operate.
  • Injector: Code to inject the dependency into the dependent class.

Use Dependency Injection in Salesforce Apex

To understand and implement dependency injection in Salesforce Apex, let’s take an example of a class that sends notifications. Instead of hardcoding the notification method, we’ll use Dependency Injection to inject the specific notification method.

  1. First, we will create a Public Interface class Notifier.
public interface Notifier {
    void send(String message);
}
  1. Now, create a class EmailNotifier that implements the Notifier interface and sends an email notification.
public class EmailNotifier implements Notifier {
    public void send(String message) {
        System.debug('Email sent: ' + message);
    }
}
  1. Now, we’ll create a class using the Notifier class to send notifications. The Notifier instance is passed in through the constructor.
public class NotificationService {
    private Notifier notifier;
    // Constructor injection
    public NotificationService(Notifier notifier) {
        this.notifier = notifier;
    }
    public void notifyUser(String message) {
        notifier.send(message);
    }
}

The class NotificationService uses the Notifier interface but does not depend on a specific implementation. The actual notifier is inserted via the constructor.

  1. In this step, we will demonstrate how to send notifications using the NotificationService class with the EmailNotifier implementation.
Notifier emailNotifier = new EmailNotifier();
NotificationService notificationService = new NotificationService(emailNotifier);
notificationService.notifyUser('Your order has been shipped!');

In the above anonymous apex code, we have created an instance of EmailNotifier, which implements the Notifier interface. This instance will be injected into the NotificationService class.

  1. Next, we instantiated the NotificationService, passing the EmailNotifier instance through its constructor. With this, the notificationService instance sends a notification message by calling the notifyUser method.