Demystifying Dependency Injection and Services in Drupal 8

Is there a way to modify the process by which Drupal 7 invokes modules without making changes to core, or to access the global variables without calling them directly? The answer is NO!

This session aims at explaining dependency injection and services in Drupal 8, which may be a bit complex but are really useful.

The introduction of object oriented programing in Drupal 8 has brought the concept of code reuse and encapsulation, allowing users to build better maintainable and structured code. One of the concepts that contribute to this is Dependency Injection and Services.

Dependency injection is an advanced software design pattern. It implements “inversion of control”, which allows reusable code to call task-specific code. With the adoption of dependency injection in Drupal 8, literally every core subsystem, or services, can be overridden and changed.

Dependency injection:

  • Removes strong dependencies that were in Drupal 7 (global variables, invoking hooks, etc.)

  • Enables us to reuse the functionality and make our services pluggable and replaceable

  • Allows us to follow best practices, such as using service container in a decoupled manner

  • Makes the code easier to read

This session will cover:

  • The problem of reusability in Drupal 7

  • An overview of Services and Dependency Injection

  • Drupal 8 core services and how to use them

  • Services container

  • How to use Dependency Injection in contrib modules

We will talk how can we use different services and Dependency Injection in different scenarios, including demos, examples and code walk throughs.

Attendees will learn:

  • Best practices of building custom and contrib module using services and dependency injection
  • Defining your own services
  • Decoupling of custom modules

 

Experience level: 
Intermediate
Speaker(s): 
Jyoti Singh
Session Time Slot(s):