In this presentation, Juhi Rathi of Valuebound has discussed “Dependency Module in Drupal 8.” It is a technique that introduces the service container, which ensures that all the dependencies will be available with the client.
----------------------------------------------------------
Get Socialistic
Our website: http://valuebound.com/
LinkedIn: http://bit.ly/2eKgdux
Facebook: https://www.facebook.com/valuebound/
Twitter: http://bit.ly/2gFPTi8
2. ● What is Dependency Injection
● Why Dependency Injection
● Pros of DI
● Types for Dependency Injection
● DI in Drupal 8
● How and where does it all happen
● Implementation in Drupal 8
Agenda
3. What is Dependency Injection
In simple terms, Dependency Injection is a design pattern that
helps avoid hard-coded dependencies for some piece of code or
software.
The dependencies can be changed at run time as well as compile
time. We can use Dependency Injection to write modular,
testable and maintainable code
8. Constructor Injection
● If the dependency is required by the class and cannot work
without it, by using constructor injection we guarantee that
the required dependencies are present.
● Since the constructor is only ever called when instantiating
our object we can be sure that the dependency can’t be
changed or altered during the object lifetime.
9. Constructor Injection
/ Constructor
public function __construct(Service service) {
// Save the reference to the passed-in service inside this client
this.service = service;
}
10. Constructor Injection Drawback
These above two points make Constructor Injection extremely
useful, however there is also a few drawbacks that make it
unsuitable for all scenarios:
● Since all dependencies are required, it’s not suitable when
optional dependencies are needed.
● While using class inheritance trying to extend and override
the constructor becomes difficult.
11. Setter Injection
● With Setter Injection the dependencies are provided to our
class after it has been instantiated using setter methods.
● Allows for optional dependencies and adding new
dependencies is as easy as adding a new setter method.
// Setter method
public void setService(Service service) {
// Save the reference to the passed-in service inside this client
this.service = service;
}
12. Interface Injection
The dependency provides an injector method that will inject the
dependency into any client passed to it. Clients must implement an
interface that exposes a setter method that accepts the dependency.
// Service setter interface.
public interface ServiceSetter {
public void setService(Service service);
}
13. DI in Drupal 8
● Drupal 8 introduces the concept of services to decouple
reusable functionality and makes these services pluggable
and replaceable by registering them with a service
container.
● Dependency injection is the preferred method for accessing
and using services in Drupal 8 and should be used whenever
possible. Rather than calling out to the global services
container, services are instead passed as arguments to a
constructor or injected via setter methods.
14. How and where does it all happen
Drupal Kernel: core/lib/Drupal/Core/DrupalKernel.php
Core Services are defined in: core/core.services.yml
All symfony dependencies related data added in :
core/lib/Drupal/Core/DependencyInjection/..
17. Reference URLs
To get all service list :
https://api.drupal.org/api/drupal/core%21core.services.yml/8.2.x
To search any service:
https://api.drupal.org/api/drupal/services
For Concepts :
https://www.youtube.com/watch?v=0SdBVmxuCZg