DEV Community

Cover image for Using Dependency Injection in Elixir
Allan MacGregor 🇨🇦 for AppSignal

Posted on • Originally published at blog.appsignal.com

Using Dependency Injection in Elixir

While controversial in functional programming, dependency injection can be a useful pattern in Elixir for managing dependencies and improving testability.

In this, the first part of a two-part series, we will cover the basic concepts, core principles, and types of dependency injection. We'll explore its benefits in terms of modularity, testability, and maintainability.

Then, we will look into a specific scenario where dependency injection can be beneficial, in this case, testing.

Let's first explain what dependency injection is.

What Is Dependency Injection?

Dependency Injection (DI) is a software design pattern that involves supplying an external dependency to a component rather than allowing the component to create the dependency itself. This pattern is a form of Inversion of Control (IoC), where control over the dependencies is inverted from the component to an external entity.

The main goal of DI is to reduce coupling between components, making our system more modular, flexible to changes, and easier to test.

These are the core concepts of dependency injection:

  • Dependency: An entity that another entity depends on to function properly.
  • Injector: The mechanism that injects dependencies into a component.
  • Client: The component that depends on the provided dependencies to operate.
  • Service: The dependency that the client component uses.

Types of Dependency Injection

  1. Constructor Injection: The dependencies are provided through the component's constructor.
  2. Setter Injection: The dependencies are provided through setter methods or properties.
  3. Interface Injection: The dependency provides an injector method that will inject the dependency into any client passed to it.

Advantages of Dependency Injection

  • Reduced Coupling: By decoupling components from their dependencies, systems become more modular, allowing for easier maintenance and scalability.
  • Increased Flexibility: Changing or updating dependencies does not require changes to the dependent components, making the system more adaptable.
  • Improved Testability: Dependencies can be easily mocked or stubbed in tests, allowing for more isolated and reliable testing.

How Dependency Injection Works

There are four steps you should take to leverage dependency injection in your program or service:

  1. Define the Service Interfaces: These interfaces represent the abstract contracts that services must fulfill.
  2. Implement the Services: Concrete implementations of the service interfaces are developed.
  3. Configure the Injector: The injector is configured to know which service implementations to inject into which clients.
  4. Inject Dependencies: When a client is instantiated, the injector supplies it with the required service implementations based on the configuration.

Dependency Injection Diagram

The following diagram illustrates the basic concept of dependency injection:

Dependency Injection Diagram

  • The Client requires a service interface to perform its function.
  • The Dependency Injector decides which implementation of the service interface (Service A or Service B) to inject into the client at runtime.
  • Service A and Service B are different implementations of the same service interface. The injector injects one of these into the client based on the configuration or conditions.

This pattern allows for high flexibility and decoupling of components within software applications, facilitating easier management, testing, and evolution of the application code.

How Can Dependency Injection Be Applied in Elixir?

As we mentioned earlier, dependency injection is a pattern that is more commonly associated with object-oriented programming languages. Functional programming languages like Elixir offer a different set of tools and idioms for managing dependencies and state. However, the principles of DI can still be applied in Elixir to achieve similar benefits.

In Elixir, the emphasis on explicit over implicit dependency management aligns well with DI principles. For testing purposes, DI allows developers to easily replace real implementations with mocks or stubs, facilitating isolated unit tests that are not dependent on external services or state. This approach enhances test reliability and execution speed, as tests become less brittle and more focused on the functionality being tested.

Practical Application of Dependency Injection in Elixir for Testing

let's look at how we can use dependency injection to inject mocks and configure dependencies in Elixir.

Injecting Mocks

One common application of DI in Elixir testing involves injecting mock modules or functions that simulate the behavior of real dependencies. This technique is particularly useful when dealing with external services like databases or APIs.

defmodule MyApp.MyModule do
  def fetch_data(dataSource) do
    dataSource.query()
  end
end

defmodule MyApp.MyModuleTest do
  use ExUnit.Case

  test "fetch_data returns expected result" do
    mockDataSource = %{
      query: fn -> {:ok, "mocked data"} end
    }

    assert MyApp.MyModule.fetch_data(mockDataSource) == {:ok, "mocked data"}
  end
end
Enter fullscreen mode Exit fullscreen mode

In this example, MyApp.MyModule.fetch_data/1 depends on a dataSource that responds to a query function. During tests, a mock dataSource is injected, allowing the test to run independently of any external data sources.

Configurable Dependencies

Another DI strategy involves using application configuration to define dependencies, which can then be overridden in the test environment.

# config/config.exs
config :my_app, data_service: MyApp.DataService

# config/test.exs
config :my_app, data_service: MyApp.MockDataService
Enter fullscreen mode Exit fullscreen mode

In your application code, you would fetch the dependency from the application configuration:

defmodule MyApp.MyModule do
  def fetch_data do
    dataSource = Application.get_env(:my_app, :data_service)
    dataSource.query()
  end
end
Enter fullscreen mode Exit fullscreen mode

This simple example shows how DI can be achieved in Elixir by configuring dependencies at runtime, allowing for easy substitution of real implementations with mocks or stubs during testing.

Next, let's review a more practical example that uses DI to inject a mock service into a module for testing.

Testing with Dependency Injection

In this example, we will work on EmailScanner, a module that scans emails for spam. We will use a SpamFilterService to check if emails are spam and dependency injection to inject a mock SpamFilterService for testing.

Start by creating a new Elixir project:

mix new email_scanner
Enter fullscreen mode Exit fullscreen mode

Now let's move on to implementation and testing.

Implementation with ExUnit

First, create the EmailScanner module. This module will depend on a SpamFilterService to check if emails are spam. In this case, the SpamFilterService will be injected as a dependency, making it easy to swap with a mock during testing.

defmodule EmailScanner do
  def scan_email(spam_filter_service, email) do
    spam_filter_service.check_spam(email)
  end
end
Enter fullscreen mode Exit fullscreen mode

Testing EmailScanner with ExUnit

Now, let's write a test for the EmailScanner module using ExUnit. We'll create a mock SpamFilterService to inject during tests:

defmodule MockSpamFilterService do
  def check_spam(_email), do: false
end
Enter fullscreen mode Exit fullscreen mode

In this mock, the check_spam/1 function always returns false, simulating a non-spam email. Next, let's create a test case that makes use of our new mock:

defmodule EmailScannerTest do
  use ExUnit.Case

  test "scan_email with non-spam email returns false" do
    non_spam_email = %Email{content: "Hello, world!"}
    assert false == EmailScanner.scan_email(MockSpamFilterService, non_spam_email)
  end
end
Enter fullscreen mode Exit fullscreen mode

This test injects MockSpamFilterService into EmailScanner, isolating the test from the real spam filtering logic and focusing solely on the EmailScanner's behavior.

By doing this, we can decouple the EmailScanner module from the SpamFilterService, making it easier to test and maintain.

Now that we've taken a look at using ExUnit and testing, let's turn to some common dependency injection mistakes to avoid and best practices.

Common Dependency Injection Pitfalls and Best Practices

First, we'll touch on some pitfalls:

  1. Over-Reliance on Mocks: While DI makes it easy to replace real implementations with mocks, overusing mocks can lead to fragile tests that are overly focused on implementation details rather than behavior.
  2. Complex Dependency Graphs: Introducing DI without careful planning can lead to a tangled web of dependencies that are hard to manage and understand.
  3. Ignoring the Complexity of Configuration: DI often requires some form of configuration to wire up dependencies. This configuration can become complex and unwieldy if not managed properly.

To help you avoid these pitfalls, here are some best practices to follow:

  1. Define Clear Interfaces: Ensure that your dependencies have clearly defined interfaces.
  2. Use Configuration Wisely: Be mindful of the complexity that configuration can introduce.
  3. Leverage Elixir’s Capabilities: Take advantage of Elixir’s features, such as module attributes and configuration files, to manage your dependencies effectively.
  4. Test with Real Implementations When Possible: While mocking is useful, also test with real implementations to ensure that your system works as expected in real-world scenarios.

That's it for this part of the series!

Wrapping Up and What's Next

In this article, we have covered the basic concepts of dependency injection, its application in Elixir, and how it can be leveraged for testing. We have also discussed common pitfalls to avoid and best practices to follow when implementing DI in Elixir.

In the next and final part of this series, we'll look specifically at advanced dependency injection in Elixir using Rewire.

Until then, happy coding!

P.S. If you'd like to read Elixir Alchemy posts as soon as they get off the press, subscribe to our Elixir Alchemy newsletter and never miss a single post!

Top comments (0)