Dependency Injection In Magento 2

Dependency Injection In Magento 2

In today’s, Blog I will explain about Dependency Injection in Magento 2, So please read this blog till end so you don’t miss any important point.

Dependency Injection is a design pattern in Magento 2 , When you have one class and and this class is dependent on another class/object to perform some functions, so it’s called Dependency and when you inject that class into dependent class then it’s called Injection.

Let’s understand Dependency Injection in simple example

Suppose you have home and you want cooling in summer so what you will do you will build your own AC or your will purchase AC and install at your home? Obviously build AC is not an easy task better to purchase AC and install at home and enjoy it’s cooling feature. So here your home is dependent class, AC becomes your home dependency and cooling is a function of that AC. So you have injected AC into your home to use cooling function. So this whole process called as Dependency Injection.

Dependency Inject provides high value concept of loose coupling. If you want to inject any particular class into another class then you can simply do it using Dependency Injection in Magento2

Methods of Dependency Injection in Magento 2

In Magento2 we have have 2 methods of dependency injection.

  • Constructor Injection
  • Method Injection

Constructor Injection

Constructor Injection is one of the popular and most commonly used Dependency Injection in Magento 2. In this type you just need to pass a parameters into class constructor to inject any dependency.

Example

<?php

namespace Mageacademy\ArgumentReplacement\Helper;

use Magento\Framework\App\Helper\AbstractHelper;
use Magento\Framework\App\Helper\Context;

class DependentClass extends AbstractHelper
{
    protected $helperClass;

    public function __construct(
        \Mageacademy\ArgumentReplacement\Helper\HelperClass $helperClass,
        Context $context
    )
    {
        $this->helperClass = $helperClass;
        parent::__construct($context);
    }


    public function getResult()
    {
        return $this->helperClass->callFunction();
    }

}
<?php

namespace Mageacademy\ArgumentReplacement\Helper;

use Magento\Framework\App\Helper\AbstractHelper;

class HelperClass extends AbstractHelper
{
    public function callFunction()
    {
        return "Some functions";
    }
}

In Above example you can see I have written a dependent class and into that class I want to call callFunction which is defined in HelperClass , So what i will do here I will inject HelperClass into my Dependent Class’s constructor. and call it’s function. So this is called Constructor Injection

Method Injection

A Dependency class which cannot be injected into constructor and may vary on each method call at that time we need to use method Injection. It’s most uses happen in api in magento 2.

Best Example of Method Injection is as below

<?php

namespace Mageacademy\ArgumentReplacement\Helper;

use Magento\Framework\App\Helper\AbstractHelper;
use Magento\Framework\App\Helper\Context;

class DependentClass extends AbstractHelper
{
    protected $helperClass;

    protected $helperClassCFactory;

    public function __construct(
        \Mageacademy\ArgumentReplacement\Helper\HelperClass $helperClass,
        \Mageacademy\ArgumentReplacement\Helper\HelperClassCFactory $helperClassCFactory,
        Context $context
    )
    {
        $this->helperClass = $helperClass;
        $this->helperClassCFactory = $helperClassCFactory;
        parent::__construct($context);
    }


    public function getResult()
    {
        $helperClassC = $this->helperClassCFactory->create();

        $helperClassC->setVariableA(4);
        $helperClassC->setVariableB(6);

        echo $d = $this->helperClassCFactory->calculateResult($helperClassC);

        $helperClassC->setVariableA(8);
        $helperClassC->setVariableB(6);


        echo $d = $this->helperClassA->calculateResult($helperClassC);

    }

}
<?php

namespace Mageacademy\ArgumentReplacement\Helper;

use Magento\Framework\App\Helper\AbstractHelper;
use Magento\Framework\App\Helper\Context;

class HelperClassA extends AbstractHelper
{
    protected $helperClassB;

  //  protected $helperClassC;

    public function __construct(
        \Mageacademy\ArgumentReplacement\Helper\HelperClassB $helperClassB,
        Context $context)
    {
        $this->helperClassB = $helperClassB;
        parent::__construct($context);
    }


    public function calculateResult(\Mageacademy\ArgumentReplacement\Helper\HelperClassC $helperClassC)
    {
        return $this->helperClassB->calculateResult($helperClassC);
    }

}
<?php

namespace Mageacademy\ArgumentReplacement\Helper;

use Magento\Framework\App\Helper\AbstractHelper;

class HelperClassB extends AbstractHelper
{
    public function calculateResult(\Mageacademy\ArgumentReplacement\Helper\HelperClassC $helperClassC)
    {
        return $helperClass->getVariableA() .' + '.$helperClass->getVariableB() . ' = ' .($helperClass->getVariableA() + $helperClass->getVariableB());
    }

}

Here you can see I have created HelperClassA and in that I am passing HelperClassA as dependency Injection HelperClassC into method CalculateResult because this method data will vary in different cases.

Types of Dependency Injection

  • Injectable
  • Newable/non-injectable

Injectable Dependency Injection

Injectable objects are singleton service objects obtained through dependency injection. Injectable objects can depend on other injectable objects in their constructor as long as the dependency chain does not circle back to the original injectable object.

Newable/non-injectable Dependency Injection

When you need new object every time rather than depending on same object from memory then you need to use non-injectable dependency Injection. This type of Object can be created whenever they needed.

For example you cannot depend on model object like Product , Sales etc bcz you need fresh object for such type of class everytime. because you need different product or order on every request so you cannot define it in advance in constructor so such type of dependency injection called as Non-Injectable injection.

Hope you like my this blog and useful for you please stay connected with us and subscribe so you will get updated always with our new topic.

Subscribe to our newsletter

Leave a Reply
Your email address will not be published. *