Home >Everything You Should Know About Dependency Injection In Magento 2

Everything You Should Know About Dependency Injection In Magento 2

Hello everyone,

Welcome to another tutorial of ours.

In this article, we will walk you through everything about the Dependency Injection design pattern – one of the most significant changes in Magento 2.

Let’s get started!

Overall

In Magento 2, Dependency Injection (DI) is a design pattern used to resolve dependencies in programming and replaced class Mage in Magento 1.

Basically, DI includes four components:

  • Service object: used to declare dependencies 
  • Client object: depend on service object and inheritance from dependency
  • Interface object: determine the method that client can use dependencies of service.
  • Injector object: perform service dependencies and provide them to client object

Dependency can be called a function of services and injection, which is an act that brings the dependency to its subject (client). Now clients can use service without building a new service.

Dependency Injection (DI) is an object management system based on Object Manager or Factory to create objects with available configuration.

DI is a design pattern that allows class A to declare dependencies so class B can supply those dependencies. Class A usually uses class Interface and provides class B to implement them in interface.

Thus, class B wouldn’t have to check dependency declaration because class A takes over, which shows the division of responsibilities in the construction of the structure. This is quite an important concept for developers to understand how Magento composes its classes.

Dependency Inversion Principle

To reduce code dependencies, you should follow the dependency inversion principle and use abstractions in your code. This means that your high-level classes shouldn’t work with low-level classes directly but should use the interfaces of them.

Using interfaces in your code also can reduce the risk of incompatibility bugs when Magento changes the underlying implementation of those interfaces. It also allows you to focus on what a class does instead of how it’s implemented.

Since the codebase of Magento follows the dependency inversion principle, you can use the di.xml file to map your own implementation of a Magento interface to a dependent class or service.

Dependency Injection Methods

There are two methods of dependency injection:

  • Through Constructor
  • Through Method

Through Constructor

For example: Magento\Catalog\Controller\Adminhtml\Product\Save

Through Method

API usually uses this method.

For example: Magento\Catalog\Api\ProductAttributeManagementInterface

Compiling Dependencies

Magento uses code compiler tools to collect all class dependency information and stores that information in files. The object manager uses that information during the class creating process to create concrete objects in the application.

The compiler helps generate service classes that do not exist in the codebase (proxies, factories, and interceptors) but are declared in code or in a configuration.

2 Types Of Dependency

Injectable

Injectable objects refer to singleton service objects obtained through dependency injection. To create injectable objects and inject them into constructors, the object manager needs to use the configuration in the di.xml file.

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

Newable objects (also known as non-injectable objects) refer to objects that cannot be injected. When they are needed, they can be obtained by creating a new class instance.

Transient objects, such as those that require external input from the user or database, fall into this category. If you try to inject these objects, you will either receive an incomplete, incorrect object or an error that the object could not be created.

Conclusion

I hope that this article is helpful for you to understand Dependency Injection.

Please Like, Share and Comment to let us know your opinion or any suggestions for this article. 

 

< Previous Post
Next Post >
+ posts