Dependency injection of Magento 2: Get to know it better

Magento 2 has been introduced and comes up with new concepts such as dependency injection. With this new design pattern, a lot of change has also been introduced within the code base, and several other new things have also been presented.

Understanding the Basis of Dependency Injection

Dependency injection is a term which gained popularity recently, especially when it comes to performing unit testing and integration. Dependency Injection has been used in Magento 2 as a substitute of Mage class used in Magento 1 for managing dependencies. DI makes coding and testing faster, easier, and absolutely transparent.

With dependency injection, there is no need for an object to locate an object or value on which it depends. Instead an environment will be created for providing dependency. As a special design pattern, it helps you implement control version and facilitates a program with an ability to follow the principles of dependency injection.

The primary goal of dependency injection is is to minimize dependence- and make it absolutely easier to manage. Everything is testable, but also maintainable and flexible. This way Magento 2 will now upgraded straightaway for the purpose of unit testing.

UMajor Elements of DI

DI mainly consists of four elements:

  • Implementation of service object
  • A client object
  • An interface connecting client with the service
  • An injector object, which injects services to the client

Advantages of Dependency Injection

  • One can apply dependency injection to legacy code as a refactoring, because it does not affect the way a code behaves.
  • It enables a client to get rid of all the knowledge concrete implementation they are required to apply.
  • It makes it possible to eliminate, or at least reduce a components dependencies.
  • It also improves the testability of a component. When dependencies are injected into a component it is possible to provide mock implementations of the classes which is being used for testing purposes

Dependency Injection in Magento 2

As mentioned above, dependency injection has been introduced as an alternative to the Magento 1.x Mage Class. The pattern has been designed to meet the specific needs of Magento 2. the platform extends the concept of dependency injection container because here it's not only about injecting dependencies but also managing it effectively.

An Overview of Constructor

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

Configuring Your Application for DI

Below are the required configurations used to run and compose the application.

  • Class Definitions- a must have component of constructor. It allows you define which type of dependency should be passed to an object.
  • Instant Configurations- defines the way an object could be instantiated and also its life cycle.
  • Abstract Implementation Mappings- define the type of implementation to be used based on the request.

Objects of DI Approach

Injectable and non-injectable are two types of objects used in DI approach. Here's a brief explanation of both of them.

  1. Injectable Objects- these are the objects belonging to the global system services and value objects. There is no need for them to identify and they don't call for any input when being created.
  2. Non-injectable Objects- These are data objects or we can say primitive values, which have an identity. Non-injectable objects cannot be created via object manager, since user interaction should be provided.

Utility Objects

In Magento 2, there are two kinds of utility objects:

  1. Factories- for factory objects
  2. Proxies- for proxy objects

This is how a constructor can be used:

 * @var Magento_Core_Model_ThemeFactory 
protected $_themeFactory; 
 * @param Magento_Core_Model_ThemeFactory $themeFactory 
public function __construct(Magento_Core_Model_ThemeFactory $themeFactory) 
    $this->_themeFactory = $themeFactory; 


Factories can be used for creating non-injectable objects. These are special objects with a single goal that is to create an instance of one non-injectable class or interface. Also, factories can rely upon the object manager. They are used to separate object manager from business code:

_objectManager = $objectManager; 
    public function create($sourceData = null) 
        return $this->_objectManager->create('Magento\Core\Model\Config\Base', array('sourceData' => $sourceData)); 

Factories are quite easy to understand, so developers feel free to play around with them.

Class Definitions

Magento makes use of class constructor signature for declaring information about class dependencies. It reads constructors using reflection, and it's the only tool recommended for using compiler tool to pre-compile class definitions.

Type Configurations

Type is basically the scope of the dependency, including all the Magento modules and module area.

Specify Types

Below is the example of dependency injection by type.


The above example showcases these three types:

  • config – virtual type extending Magento/ Core/Model/Config
  • moduleConfig – extends Magento/Core/Model/Config
  • Magento/Core/Model/App – retrieves moduleConfig as a dependency


While constructing arguments are injected into a class. Parameter name should be in tune with the parameters of the configured class.

Parameter – declared in the constructor signature variable
Argument- it's basically a value which is being used when a class is being created.

Final Words

Magento 2 is coming up with Dependency Injection and it will going to be very useful when it comes to working with tightly coupled code and component separations.

We always keep up with the latest Magento 2 updates. Right after the release of final version we’ll provide an opportunity of migration to Magento 2 or upgrade for current shopping cart version.

Jason Roiz is a web developer by profession and a writer by hobby and works for OSSMedia, a Magento development company. He loves sharing information regarding Magento development tips & tricks. If you are looking forward to hire Magento developers then just get in touch with him.