Dependency Injection is one of the most talked feature of Magento 2.  There have been major code changes in Magento 2 to implement in Dependency Injection. To understand the Magento 2 Dependency Injection, we first have to understand the Dependency Injection in general.

Dependency Injection in General :

A dependency is an object that can be used and an injection is the passing of a dependency to a dependent object that would use it. See the method below.

Screen1

There is nothing wrong with this method. It should return the desired value without any issues. When it comes to reusing this method, there are challenges. This method depends on DBConnection and hence it will force to use DBConnection  for every client call to getSkuById().

Lets consider that you want to use a new test database TestDBConnection. In that case, you have to do code modification in this method.

So to overcome this hard coded problem, you can inject the dependencies into the method through the parameters.

Screen2

Now this method is no longer dependent on the DBConnnection object. Now the clients using getSkuById() can initiate and pass the object to getSkuById() method as parameters.

However the dependency problem has not yet been resolved fully. We have just pushed the dependency to one level up and now each client using getSkuById() has became dependent. See an example of the client using getSkuById(). Here, view class is now dependent on DBConnection.

Screen 3

There are two solutions to overcome this problem.

  • One  is to continue the dependency injection all the way up through the layers.
  • Second is to use automatic dependency injection. With Automatic DI, an object does not need to locate an object or value in which it depends.

There are three types of Dependency Injection: Constructor Injection, Setter Injection and Interface Injection. Magento 2 uses Constructor Injection. See the explanation of DI types in Wiki.

Dependency Injection in Magento 2:           

Points to note about Magento 2 Dependency Injection.

  • Dependency Injection is an alternative to Mage class used in Magento 1.
  • Magento 2 uses Automatic Dependency Injection.
  • Magento 2 uses Constructor Dependency Injection.
  • Using Constructor for DI is a must.

The above given example will look like this in Magento 2.

Screen4

Here you see how Dependency is being injected to the constructor.

Screen5

In view class, we are calling getSkuById() but no where we are passing the database object to the constructor. This is being done by the Object Manager behind the scene. When we invoke the create() method of Object Manager, it creates the object for Product class, gets the value for the constructor parameter from instance configuration (one of the di.xml)  files and passes it to the constructor.

Instance Configurations can be specified at three levels:

  • app/etc/di/*.xml
  • {moduleDir}/etc/di.xml
  • {moduleDir}/etc/{areaCode}/di.xml

Inside di.xml :Attributes in di.xml

Screen6

type name : This is the name of the class to which the parameter has to be passed. In our example, this the Product Class.

argument name : This should be the name of the parameter or argument used in constructor. In our case. it is db.

xsi:type : This should be the type of the argument to be passed to the constructor. In our case, it is an object.

So when we invoke the create method of ObjectManager, object manager reads this di.xml file component and passes the Object of the DBConnection class to the constructor of the Product class.

There are other components of di.xml and that calls for a separate blog. In the next part of the blog, I will dig deeper and discuss factories, proxies and object manager in dependency injection.

Be the first to Know

Subscribe to our blog to get the latest articles directly to your inbox.

READY TO DRIVE SALES?

Request a Quote Today

Request a Quote

Leave a Reply

Be the First to Comment!