Hikmadh Commerce| Ecommerce Development Consulting | Magento Development and Consulting Service

Understanding the di.xml

In Magento 2, `di.xml` is an important configuration file used for Dependency Injection (DI). DI is a design pattern that allows objects to define their dependencies externally, rather than creating them internally. It promotes loose coupling and modular design, making the code more maintainable, testable, and flexible.


The `di.xml` file is located in the `/etc` directory of a Magento 2 module. It defines how Magento’s object manager (also known as the dependency injection container) should instantiate and inject dependencies into various classes within the module.


 

Dependency injection

Magento 2 uses Dependency Injection to replace functionality provided by the Mage class in Magento 1.x.

 

Dependency Injection is a design pattern that allows an object A to declare its dependencies to an external object B that supplies those dependencies. The dependencies declared by A are usually class interfaces and the dependencies B provides are concrete implementations for those interfaces.

 

This allows for loose coupling of code because object A no longer needs to be concerned with initializing its own dependencies. Object B decides which implementations to provide to object A based on a configuration or desired behavior.

 

This is an important concept to understand for extension developers because it forms the basis of how Magento composes its classes.

Dependency inversion principle

Follow the dependency inversion principle dependency inversion principle and use abstractions in your code to reduce code dependencies. This means that your high level classes should use the interfaces of low level classes instead of working with them directly.

 

Using interfaces in your code reduces the risk of incompatibility bugs when Magento changes the underlying implementation of those interfaces. It also lets you focus on what a class does instead of how its implemented.

 

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

Here are some key aspects and functionalities of `di.xml` in Magento 2:

  1. Preference: The “ node in `di.xml` allows you to specify a different implementation (class) for an interface or a class. It tells the object manager to use the specified implementation whenever an instance of that interface or class is requested.
  2. Virtual Types: The “ node allows you to define a new type of object that is created on-the-fly by the object manager. It’s useful when you need to configure complex objects with constructor arguments or method calls.
  3. Type Configuration:The < type > node is used to configure constructor arguments, method calls, and property values for a specific class. It allows you to override the default behavior of a class or add additional functionality by defining plugins, interceptors, and observers.
  4. Arguments: The “ node within a `< type >` node specifies the values for constructor arguments of a class. It can include scalar values, references to other classes or objects, and configuration parameters.
  5. Plugins: The “ node within a `< type >` node is used to define a plugin for a specific class. Plugins enable you to modify the behavior of public methods of a class by intercepting their execution before, after, or around the original method.
  6. Interceptors: The “ node within a `< type >` node allows you to define an interceptor for a class. Interceptors wrap around the original class and provide a way to modify its behavior by intercepting method calls.
  7. Observers: The “ node within an `< event >` node is used to configure observers for events in Magento. Observers are classes that respond to specific events triggered during the execution of the system. They allow you to add custom logic or actions when an event occurs.

Understanding Magento 2 Dependency Injection

What Are the Different Types of Dependency Injection in Magento?

By using Automatic Dependency Injection (ADI), the object doesn’t need to look for another dependent object. There are three types of Automatic Dependency Injections:


  1. Constructor Injection: The most common way to inject dependencies is via a class’s constructor. To do this, you need to add an argument to the constructor signature to accept the dependency.

  2. Setter Injection: Another possible method to inject a dependency into a class is by adding a setter function that takes the dependency.

  3. Interface Injection:The interface injection defines and uses interfaces for the dependency injection.

`di.xml` is a powerful configuration file that controls the behavior and interactions of classes within a Magento 2 module. It plays a crucial role in Magento’s dependency injection framework, allowing for extensibility, customization, and modular development.


Magento 2 uses Automatic Dependency Injection (ADI) which is further derived to the Constructor Injection. It means in Magento 2, passing the objects in the constructor’s parameters will help you follow the injection rules. Dependency Injection in Magento 2 is an alternative to the Mage Class method of Magento 1. The below example will give you an idea that how it will look like in Magento 2.

Why Magento 2 Dependency Injection is Preferred Over Object Manager?


The Object Manager can also do this magic. We can call the create() method of ObjectManager, to create the object for your class, gets and passes the value for the constructor parameter from instance configuration (di.xml) files. But you should never use ObjectManager!!


It is still used somewhere in the core of Magento 2, but it will get refactored soon. So, the best recommendation is, DO NOT USE THE OBJECTMANAGER. It is a good practice to know what the code depends on, rather than having hidden dependencies in the middle of the code. That is why Magento 2 Dependency Injection is preferred by the structure.

Conclusion

It is necessary to follow the code patterns while doing development on Magento 2. Magento Community recommends using Magento 2 Dependency Injection rather than ObjectManager. It was a short introduction of Dependency Iinjection, Automatic Dependency Iinjection, Constructor Injection, and Object Manager in Magento 2.