Ce diaporama a bien été signalé.
Nous utilisons votre profil LinkedIn et vos données d’activité pour vous proposer des publicités personnalisées et pertinentes. Vous pouvez changer vos préférences de publicités à tout moment.

Dependency Injection in Drupal 8

68 vues

Publié le

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

Publié dans : Technologie
  • Soyez le premier à commenter

  • Soyez le premier à aimer ceci

Dependency Injection in Drupal 8

  1. 1. Juhi Rathi https://www.drupal.org/u/juhi-rathi Dependency Injection
  2. 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. 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
  4. 4. Why Dependency Injection
  5. 5. Pros of DI ● Decoupling ● Cleaner Code ● Reusable & flexible code ● Testable code
  6. 6. ● Constructor Injection ● Setter Injection ● Interface Injection Types of Dependency Injection
  7. 7. 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.
  8. 8. Constructor Injection / Constructor public function __construct(Service service) { // Save the reference to the passed-in service inside this client this.service = service; }
  9. 9. 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.
  10. 10. 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; }
  11. 11. 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); }
  12. 12. 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.
  13. 13. 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/..
  14. 14. Implementation in Drupal 8
  15. 15. 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