Magento 2 Dependency Injection

Magento 2 Dependency injection is used to replace the Magento 1.x Mage class when you convert to work with Magento 2.

Until now, I’m sure that all of the Magento 1 users have moved their stores to Magento 2 already. To work with Magento 2, it’s necessary to use Magento 2 Dependency injection that is used to replace Magento 1.x Mage class.

The Dependency injection design pattern creates an external environment where you can inject dependencies into an object. Thanks to that, you will no longer have to create the objects manually. Namely, as when object A calls object or value B, this means B is a dependency of A.

Table of contents

Magento 2 Dependency Inversion Principle

If you are working with Magento 2 Dependency Injection, you should take look at Magento 2 Dependency Inversion Principle because this principle will restrict the direct working between the high level and low level classes. At that time, the interaction will implement via an interface of the low level classes as an abstract layer.

Specifically, the di.xml file takes responsibility for mapping an interface dependency to a preferred implementation class. It can be said that with Magento 2 Dependency Inversion Principle, the dependency of the coding will be reduced significantly due to the abstract layer.

Object manager - Dependency Injection Container

Object Manager is called as Dependency Injection Container, Magento 2 service class which contains and handle the dependencies between the objects. During the class construction, the object manager injects the appropriate dependency as defined in the di.xml file.

Constructor signature dependencies

In Magento 2, the class definition use constructor signature to get information (type and number of dependencies).

Compiling dependencies

All information related to Magento 2 Dependency Injection are collected in a class and saved in files by a code complier tool. And then the ObjectManager will get this information to generate concrete objects in the application.

Injection types used in Magento 2

Magento 2 Dependency Injection includes two types: Constructor Injection and Method Injection. You can see the following code snippet to learn more about both of them.

namespace Magento\Backend\Model\Menu;
class Builder
{
    /**
     * @param \Magento\Backend\Model\Menu\Item\Factory $menuItemFactory
     * @param \Magento\Backend\Model\Menu $menu
     */
    public function __construct(
        Magento\Backend\Model\Menu\Item\Factory $menuItemFactory,  // Service dependency
        Magento\Backend\Model\Menu $menu  // Service dependency
    ) {
        $this->_itemFactory = $menuItemFactory;
        $this->_menu = $menu;
    }

    public function processCommand(\Magento\Backend\Model\Menu\Builder\CommandAbstract $command) // API param
    {
        // processCommand Code
    }
}

Constructor injection

As the above example, $menuItemFactory and $menu are the dependencies that will be added to an object’s class through the constructor injection. Besides, remember that the constructor injection is required to declare all optional and required of an object.

Method injection

About Method Injection, you will use it when an object makes clear a dependency in one of its methods. As if tracking in the referred instance, $command is the dependency passed into the class through the processCommand method.

Groups of Object

In Magento 2, the object is divided into two groups: injectable and non-injectable (newable) objects. What are they?

Injectable Objects

About the injectable Objects, you can call as services or objects which will show the dependencies in their constructors and are created by the object manager via the configuration in the di.xml file. And you can use these injectable objects to request other injectable services in the constructors.

Non-injectable Objects

Non-injectable (Newable) Objects are a bit similar to the injectable objects when they also expose the dependencies in their constructors, however, the newables are allowed to request other newables objects like Entities, Value Objects. In addition, you cannot demand the newable objects for keeping a reference to an injectable object.

This is the detialed information related to Magento 2 Dependency Injection design pattern. Wish you have a great time with it!

Related Topics

Enjoyed the tutorial? Spread it to your friends!

magento-2-tutorial
dependency
injection
featured

Sam Thomas
Sam Thomas

CEO and Founder of Mageplaza. Pursueing a simple and healthy lifestyle. A friend, a husband and a dad of two children, a trainer and an influencer wannabe. He is a big fan of sports and travel, also.

People also searched for

  • magento 2 dependency injection
  • dependency injection in magento 2
  • 2.2.x, 2.3.x, 2.4.x