Magento 2 Dependency Injection

Magento 2 Dependency Injection: Everything You Should Know About

by Lily Tran

Hello everyone,

Welcome to another tutorial of ours.

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

Let’s get started!

Magento 2 Dependency Injection: Overall

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 the client can use dependencies of service.
  • Injector object: perform service dependencies and provide them to the 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 the service without building a new service.

Dependency Injection in Magento 2 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 the 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 their interfaces.

Using interfaces in your code can also 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 implementation of a Magento interface to a dependent class or service.

Magento 2 Dependency Injection Methods

There are two methods of dependency injection in Magento 2:

  • 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 store 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 a configuration.

2 Types Of Magento 2 Dependency Injection

In this tutorial for Magento beginners, we will show the 2 types of dependency injection: Injectable and non-injectable

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

In conclusion, Magento 2 Dependency Injection (DI) is vital for improving code flexibility, reusability, and efficiency. By leveraging DI, you can manage object dependencies more effectively, leading to cleaner, more maintainable code. To fully harness the benefits of Magento 2 DI in your projects, becoming a partner with a trusted Magento expert is key.

BSS Commerce offers comprehensive Magento extensions, solutions, and expert guidance to help you implement your online store and other Magento 2 best practices efficiently.

Explore more with BSS Commerce to optimize your Magento store!

Next Reading Suggestions

© 2019 BSS Commerce owned by THANH CONG INTER ., JSC. All Rights Reserved.
Business registration certificate no. 0106064469 issued by Hanoi Department of Planning and Investment on 19 December 2019.
Legal Representative: Mr. Nguyen Quang Trung.