Drupal 8 Design Patterns: Dependency Injection

Drupal 8 makes extensive use of object-oriented design patterns to create readable, reusable, and testable code. Being able to recognize these patterns and understand their purpose is essential to working with any OO system, let-alone Drupal 8.

This session will discuss the design patterns utilized in Drupal 8 with particular emphasis given to the Dependency Injection (DI) pattern. We'll explain the pattern, why its so valuable, and how its used in Drupal 8 core. We'll then introduce the Dependency Injection Container (DIC) and how to use it as a module developer by demonstrating its practical application in unit tests.

The following questions will be answered in this session:

  • What is Dependency Injection?
  • What problems does it solve?
  • What is a Dependency Injection Container?
  • Why is this relevant to me as a module maintainer?
  • What are compiler passes?
  • How can I use Dependency Injection to write unit-testable code?
  • What are some best practices around Dependency Injection that I should be aware of?

This session is based on the excellent presentations given by Katherine Bailey at Drupal Developer Days Dublin 2013 and DrupalCon Portland. Slides adapted and used with permission.


Experience level