×

In layman's terms, Services & Dependency Injection in Drupal

Read Article
Pause
Resume
Stop
In layman's terms, Services & Dependency Injection in Drupal

If we look into the basis of Software engineering, Dependency Injection is an approach in which one object get another object that it depends on. And these other objects are called dependencies. And Injection refers to the passing of a dependency (as a service) into the client object that would use it.

With Dependency Injection, the class is no longer responsible for instantiating the objects it requires. Those responsibilities are passed to independent services.

There are a few advantages and disadvantages to having dependency injection.

  • Dependency Injection will decrease the coupling between a class and its dependency. It is one of the advantages.
  • For a developer, it will be difficult to trace the code because it separates behaviour from construction. It’s one of the disadvantages.

In Drupal terminology, a service is any object managed by the services container. And Drupal introduced a concept of services that divide re-usable functionalities and makes them into services (which are pluggable) and registering them with a service container.

As a Drupal Developer, you have to make sure a couple of things while working Drupal 8 or later versions of 8.

  • Always access any drupal service via a service container to make sure decoupled nature of these systems is respected.
  • If you want to trigger an email or do any DB operations, use the related services via service container, which will do needs without any hassle.
  • And one more advantage is, the code which you write will be easier to test via PHP Unit tests because your logic will be separate from the drupal dependencies.

And there is one more concept related to this Services & Dependency Injection, which is called Autowiring. It is a Symfony framework feature & not automatically tested by the Drupal Core test suite and there are no Drupal Core services that are autowired.

But, What Symfony says about Autowiring is, Autowiring allows us to manage services in a container with minimal configuration and It reads type-hints on a constructor and automatically passes correct service to each method.

And since Drupal 9 PHP classes are not auto resolved, If your services are autowired but the injected classes are not. In such cases, it’s better to write your own service providers for the module than having a .services.yml file in the module.

And in Drupal, how to inject dependencies into controllers, forms & blocks

  • In Drupal every object uses the ContainerInjectionInterface interface, you can implement create() factory method to inject the dependencies into the class constructor.

This way the concept of Dependency Injection & Services are been utilized in Drupal.

Comments

Scroll to Top
We Need Your Consent
By clicking “I Accept Cookies”, you agree to the storing of cookies on your device to enhance site navigation & analyze site usage.
I Accept Cookies