The Most Popular Extension Builder for Magento 2

With a big catalog of 224+ extensions for your online store

The Basics of Dependency Injection in Magento 2

Dependency Injection is a unique design pattern that implements control inversion and provides the ability to follow the principle of dependency inversion. It is a technique that enables loose coupling. A number of the latest software application frameworks support Dependency Injection including TypeScript, Spring, Google Guice, Microsoft Managed Extensibility Framework (MEF), etc.

In Magento 2, one of the most significant changes is the use of Dependency Injection. With this design pattern, the codebase of Magento 2 has changed a lot, and many new things have been introduced. And now it has the same functionality as provided by the Mage class in Magento 1.

In this article, I will explain the basics of Magento 2 Dependency Injection in Magento 2 to help those who are new to this topic.

What is Dependency Injection?

Dependency is the object which is required by the class from an external source and injection is the method of passing that particular dependency to the dependent class. For example, if you have a class that fetches some data by using Magento 2 Observer Class, we can say that your class has a dependency on that Magento 2 Observer Object.

Still confused? Take a look at this fantastic example by John Munsch, who has explained dependency injection to a 5-year-old:

“When you go and get things out of the refrigerator for yourself, you can cause problems. You might leave the door open or get something Mommy or Daddy doesn’t want you to have. You might even be looking for something we don’t even have or which has expired. What you should be doing is stating a need, “I need something to drink with lunch,” and then we will make sure you have something when you sit down to eat.”

It means that the collaborating classes (5-year-old) should rely on the foundation classes (parents) to provide the desired results. Dependency Injection is a win-win situation for everyone, it passes the object to the dependent class, rather than allowing the dependent class to build or find the object from scratch.

Types of Dependency Injection in Magento 2

Magento 2 Dependency Injection provides a high-value concept of loose coupling modules together. If you want to inject dependency into a particular class, you can simply do it by using two types of Dependency Injections in Magento 2.

Constructor Injection

Constructor Injection is one of the commonly used ways to inject dependencies in Magento 2. In this type of DI, all you need to do is add a parameter in the class constructor to inject the dependency. It should be the first choice because Constructor Injection addresses the most simple scenario where a class requires one or more dependencies.

For example, I have a Helper class that can be found in Namespace/ModuleName/Helper/Data.php file with the following code:

<?php
namespace Namespace\ModuleName\Helper;
use \Magento\Framework\App\Helper\AbstractHelper;
class Data extends AbstractHelper
{
       public function HelperDemo()
       {
               //Do Something Here
       }
}

In the above Helper Class, I have created a function HelperDemo() that can be called anywhere in Magento 2 using the Constructor Injection.

<?php
class DependentClass
{
       public function __construct(Namespace\ModuleName\Helper\Data $helper)
       {
               $this->helper = $helper;
       }
       public function MyFunction()
       {               $this->helper->HelperDemo();
       }
}

In the above code sample, the DependentClass declares its dependency on the Helper class in its constructor. This way I can call the HelperDemo() function anywhere within the class.

Method Injection

Method injection is another type of Magento 2 Dependency Injection, which passes the dependency as a method parameter to use it in the class. Method injection can be used best when the dependency may vary on each method call. When an object requires performing specific actions on a dependency that cannot be injected, it is recommended to use the Method Injection.

<?php
namespace Namespace\ModuleName\Observer\Product;
use Magento\Framework\Event\ObserverInterface;
class Data implements ObserverInterface
{
    /**
     * @param Magento\Framework\Event\Observer $observer
     */
    public function execute(Magento\Framework\Event\Observer $observer)
    {
        //Do Something Here
    }
}

In the above example code for Method Injection, note that the execute() method/function in Data class is dependent on the Magento\Framework\Event\Observer class.

Setter Injection

Setter injection involves injecting dependencies through setter methods. In this approach, the class provides setter methods that allow you to set the dependencies after the object has been created. Here’s an example:

class MyClass
{
    private $dependency;
    public function setDependency(Dependency $dependency)
    {
        $this->dependency = $dependency;
    }
    public function someMethod()
    {
        // Use the dependency
        $this->dependency->doSomething();
    }
}

How important is Magento 2 Dependency Injection?

In general, Magento promotes Dependency Injection as a preferred approach for better code modularity, testability, reusability, flexibility, and so on. It helps developers build robust and maintainable applications while ensuring a high level of code quality and performance:

  • Modular and Testable Code: Dependency Injection helps in creating modular and loosely coupled code. By injecting dependencies instead of directly instantiating the within a class, it becomes easier to replace or mock dependencies during testing. This improves code maintainability and enables more efficient unit testing.

  • Code Reusability: With Magento 2 Dependency Injection, dependencies can be shared and reused across multiple classes. Instead of duplicating code to create instances of dependencies within different classes, dependencies can be injected and shared, reducing redundancy and improving efficiency.

  • Flexibility and Scalability: Dependency Injection allows for flexibility in managing dependencies. It becomes easier to switch or modify dependencies without making significant changes to the code. This promotes scalability as the system can easily accommodate changes or additions to the dependencies as the application evolves.

  • Decoupling of Components: Magento 2 Dependency Injection helps in decoupling components by removing direct dependencies between classes. This promotes the Single Responsibility Principle (SRP) and allows each class to focus on its own functionality, making the codebase more modular and easier to understand and maintain.

  • Magento encourages the use of Dependency Injection over direct use of Object Manager: In fact, Object Manager also offers quite same service to Dependency Injection. However, in spide of the benefit in less codes to write, Object Manager doesn’t follow the Develop Processes of Magento 2, and it even creats the unrequired hidden dependencies. Knowing what the code depends on is a better approach than facing hidden deprendencies in the code. That is the reason why the structure prefers Magento 2 Dependency Injection

Why do people prefer Magento 2 Dependency Injection to Over Object Manager?

Although the create () method of Object Manager can create your class’s objects, get, and pass the value for constructor parameter from di.xml files, we don’t recommend using it.

Object Manager goes against the objective of Dependency Injection. Even though Object Manager requires less code, it doesn’t follow the M2 Development Processes. Moreover, it creates hidden dependencies that are not required.

On the other hand, Dependency Injection has no hidden dependencies in the code. That’s why it’s a better choice.

Conclusion

Magento 2 Dependency Injection is a great way to reduce tight coupling between application codebases. Rather than hard-coding dependencies, you can inject the list of object that a class may need. Dependency Injection also enables you to manage future changes and other complexity in your codes.

Also, it is essential to follow the design patterns and coding standard while developing Magento 2. Although there’s more to learn about this topic, these were only the basics to get you started with Dependency Injection in Magento 2.

If you have any questions, please leave us a comment or connect us and Mageplaza will get back to you!

Image Description
Hello, I'm the Chief Technology Officer of Mageplaza, and I am thrilled to share my story with you. My deep love and passion for technology have fueled my journey as a professional coder and an ultra-marathon runner. Over the past decade, I have accumulated extensive experience and honed my expertise in PHP development.
Website Support
& Maintenance Services

Make sure your store is not only in good shape but also thriving with a professional team yet at an affordable price.

Get Started
mageplaza services
x
    Subscribe

    Stay in the know

    Get special offers on the latest news from Mageplaza.

    Earn $10 in reward now!

    Earn $10 in reward now!

    comment
    iphone
    go up