Have you understood about the key aspect of dependency injection in Magento 2?

Magento 2 was released with many new changes. And one of the biggest changes that online users will witness is the use of Dependency Injection which is a design pattern and takes responsibility for managing dependencies as a substitute of Mage class used in Magento 1

What is Dependency Injection?

Dependency Injection is a part of solid dependency injection. It makes sure that all the dependent are decoupled points which create a system as and when they are combined with a system. It helps Magento programmers to implements version control and provides a program with an ability to follow the principles of dependency inversion.

Dependency Injection in Magento 2

Dependency Injection in Magento 2 is an alternative of Mageno 1.x Mage class. The process of reducing the coding dependencies can be called dependency inversionThe basic principle of dependency inversion is that both high and low modules should rely upon the services offered by Magento 2. Details will depend on abstractions, whereas abstractions will work independently.

Constructor Injection

In Magento 2, constructors response for creating class dependencies. All the needed dependencies are to be declared directly in the constructor signature.

The basic objective of constructor injection is to be used with both required as well as optional service dependencies of an object. For the expensive ones, proxy patterns could be used. The best part is all of them are auto-generated and do not require you to deal with complicated coding.

Below is how a sample proxy could be declared in di.xml:

<!--?php

<type name="Magento\Backend\Model\Config\Structure\Element\Iterator\Field" shared="false"-->

<arguments>

<argument name="groupFlyweight" xsi:type="object">Magento\Backend\Model\Config\Structure\Element\Group\Proxy</argument>

</arguments> 

<!--?php

class Foo
{
protected $_bar;
public function __construct(Bar $bar)

{
$this--->_bar = $bar;
} 

public function execute()
{
//some code
$this->_bar->execute();
//some code
}
}

$bar = new Bar();

$foo = new Foo($bar);

$foo->execute();

Method Injection

Method injection needs for API objects which your objects are dependent upon.

The example below will explain the type of injection to declare $menu and $itemFactory as service dependencies and $command is used as an API parameter for your objects.

<!--?php

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

{

if (!isset($this->_commands[$command->getId()])) {

$this->_commands[$command->getId()] = $command;

} else {

$this->_commands[$command->getId()]->chain($command);

}

return $this;

}

}

Configuring your app for Dependency Injection

Before configuring, you have to make sure all the requirements below:

  • Class Definitions: an dispensable component of constructor, which help define type of dependency passed to an object
  • Instant Configurations: defines the way an object could be instantiated and also its life cycle
  • Abstract Implementation Mappings: defines the type of implementation to be used based on the request.

Class Definitions

Class constructor signatures are used for specifying the information about the class dependencies. It ensures use of reflection ad is always beneficial to use the Magento definition compiler tool to pre-compile class definitions to ensure a high level of performance.

Arguments

At the time of creation, arguments need to be injected into the class instance. In addition, the name of the parameters has to be compatible with the class constructor parameters.

  • Parameter: is declared in the constructor signature variable
  • Argument: is a value used during the process of creating a class

Dependency Injection is an important part in Magento 2 which brings multiple advantages for those opting for Magento development services. It is also a main step of improving coupled code and components separators tightly.