8

I am new in Magento 2.

I wanted to know, what is dependency injection in magento 2.

What the concept is about, and how to use it in our framework ?

I have referred this documentation

But still haven't got the concept correctly.

Marius
199k55 gold badges431 silver badges837 bronze badges
asked Mar 29, 2017 at 7:47

2 Answers 2

12

Inside a project, classes interact with each other.
So we can say, a class depends on other classes.
And there are 2 ways the connection can be made.

  1. The class locates its dependencies.
  2. The dependencies are provided to the class.

Magento 1 takes the first approach. Here is a random example. In this example, the category model depends on the url rewrites instance. So it instantiates it.

Magento 2 takes the second approach.

When building a class, all the dependencies are specified in the constructor.
When used, the class is instantiated via a factory method that reads the signature of the class constructor, to check what dependencies are required.

Magento 2 also has a dependency injection container (DIC) which is basically a big array with the classes that get instantiated.
When trying to instantiate a class, it checks if it's dependencies are already present in the DIC.
If they are, they are provided to the constructor, if not, the dependency class is instantiated in the same way as the current class you are trying to instantiate.

This is it in a nutshell. In practice is a bit more complicated because configuration files are involved also (di.xml), some dependencies might be interfaces which cannot be instantiated, but this factory method supplies a class that implements that interface when your class gets instantiated, based on the contents of the same di.xml file.
Also some dependencies are not objects. They can be arrays, string, ... They are supplied to the class via the same di.xml file.

If you want to dig and see how it actually happens, you can start from this class https://github.com/magento/magento2/blob/develop/lib/internal/Magento/Framework/ObjectManager/Factory/Dynamic/Developer.php.
But it's not an easy thing to follow. You need some patience.

answered Mar 29, 2017 at 7:58
1
  • Thnx Mauris. It cleared, the concept. Commented Mar 29, 2017 at 8:04
2

In programming languages, a dependency is known as the object which is needed by a class to perform some functions. Injection is the passing of that particular dependency to a dependent object/class. The real meaning of Dependency Injection is to inject the dependency into the class from another source.

For example, if you have defined a Class in Magento 2 that fetches some data by using Magento 2 Helper Class, we can say that your Class has a dependency of that Magento 2 Helper Object.

let's take an example :

class Customer
{
 private $firstName;
 private $lastName;
 public function __construct($firstName, $lastName)
 {
 $this->firstName = $firstName;
 $this->lastName = $lastName;
 }
 public function getFirstName()
 {
 return $this->firstName;
 }
 public function getLastName()
 {
 return $this->lastName;
 }
}
class CustomerInfo
{
 private $customer;
 public function __construct($customerFirstName, $customerLastName)
 {
 $this->customer = new Customer($customerFirstName, $customerLastName);
 }
 public function getCustomer()
 {
 return $this->customer;
 }
}

There is nothing wrong with the above code. It will return the first & last name without any issues. But the Customer class is tightly coupled with the CustomerInfo class. If you add a new parameter to the constructor of Customer class, then you have to modify every class where you had created a Customer object with the new operator, which subjects it to a tedious and lengthy process, especially when it comes to the large applications.

Dependency Injection solves these issues by injecting the dependencies through the dependent class’ constructor. The result is highly maintainable code which can be used in long-term projects. It might look like this:

class Customer
{
 private $firstName;
 private $lastName;
 public function __construct($firstName, $lastName)
 {
 $this->firstName = $firstName;
 $this->lastName = $lastName;
 }
 public function getFirstName()
 {
 return $this->firstName;
 }
 public function getLastName()
 {
 return $this->lastName;
 }
}
class CustomerInfo
{
 private $customer;
 public function __construct(Customer $customer)
 {
 $this->customer = $customer;
 }
 public function getCustomer()
 {
 return $this->customer;
 }
}

In the above code, instead of instantiating dependency with the new operator, we defined it in a __construct parameter. It is known as Automatic Dependency Injection.

answered Jan 13, 2020 at 9:55

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.