How Do Dependency Injection Work in Magento 2.x or Adobe Commerce

The Dependency injection (DI) means A class has dependency on another class B to provide some data or results, A class uses some methods of Class B to fulfill Business requirement.

Injection is process of Passing required dependency Parameter in constructor class or constructor method.

Magento 2.x or Adobe Commerce promotes dependency injection as a preferred approach for better code modularity, testability, reusability, and flexibility. Benefit of Dependency Injection

The following below two type Dependency Injection

1 – Magento 2 constructor injection
Constructor Injection is the preferred way to inject dependencies in Magento 2, as it addresses the most simple scenario where a class requires one or more dependency parameter

Constructor Injection To Pass or add a parameter in the class constructor to inject the dependency

<?php
namespace John\Mage2db\Block\Data;
 
class Data extends \Magento\Framework\View\Element\Template
{
           protected $_customerSession;
           protected $_viewHelper;
 
	public function __construct(
                \Magento\Framework\View\Element\Template\Context $context,
                \Magento\Customer\Model\Session $customerSession,
                \Magento\Customer\Helper\View $viewHelper
            )
	{

        /* dependency injection of customerSession and viewHelper for Data class */

                       $this->_customerSession = $customerSession;
                       $this->_viewHelper = $viewHelper;
		parent::__construct($context);
	}
 
	public function getCustomer()
	{
	      return  $this->_customerSession->getData(); // retrive customer data
 
	}
                public function getName()
               {
   /* To Get customer name with the help of viewHelper object's dependency */

  /* injection using Customer data */

                   return $this->_viewHelper->getCustomerName($this->getCustomer());
 
                }
}
?>

Above code retrieve customer data by using customer session &

customer session (\Magento\Customer\Model\Session $customerSession) parameter passing inside constructor method

Magento 2 Method injection
Method Injection involves passing in a dependency as a method parameter to use it in the class logic.

When a particular method is dependent on class then we have to pass that class as dependency in our method, we should use Method Injection

<?php
namespace John\Mage2db\Observer;
 
use Magento\Framework\Event\ObserverInterface;
 
class OrderNumber implements ObserverInterface {
 
    /**
     * @param Magento\Framework\Event\Observer $observer
     */
    public function execute(\Magento\Framework\Event\Observer $observer) {
        $order = $observer->getEvent()->getOrder();
        // Do Something Here
    }
}
 
?>

In the above Example, class parameter execute() retrieve Order Number by using Events / Observer & it completely depends on class

Magento\Framework\Event\Observer $observer

that’s why execute() passing class (Magento\Framework\Event\Observer $observer) as parameter to accomplish operation.

Leave a Reply

Your email address will not be published. Required fields are marked *