Dependency injection example

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.

Who is this video for?

  • Developers

Video content

Transcript
Let’s take a look at how dependency injection works and how classes are sent to the constructor.
First, we open the class, Product dot php, which is Magento, Catalog, Model, Product dot php.
It’s a product model, and as we look at its constructor, we can see a lot of different parameters, so, it may not be clear what class will be passed to the constructor. The dependency injection mechanism is responsible for generating instantiated classes and passing them as parameters.
Now, we’ll look at how we can use dependency injection to affect the parameters of a class. There’re basically three ways. The first way is to define which classes correspond to certain interfaces. For example, you can see here Magento, Store, Model, Store Manager Interface, and Magento, Catalog, API, Product Attribute Repository Interface. Each of these have a parameter. Of course, an interface cannot be instantiated, so there should be a class that implements the interface. Using di, we can figure out exactly, which class is being used. To do that, we need to look at the di dot xml file, specifically Magento, Catalog, etc, di dot xml, and search for product attribute repository interface.
Here you can see a line in the di dot xml file, that references Magento, Catalog, API, Product Attribute Repository Interface for which it substitutes the Magento, Catalog, Model, Product, Attribute, Repository class, and this specific class is assigned to this parameter, Metadata Service.
A second example of what can be done in the di dot xml, is to define a specific parameter for a specific class. In the example we’ve just seen, every class that requires an interface, will get an instance of this class. Sometimes though, you want to be able to define a specific instance for a specific class. For example, for product, you may need a particular class, that is applicable only for products. A common example in Magento is the data parameter. As you can see, the data parameter has nothing inside of it, but by using di dot xml, we can add something to that array. You can do this for any model, which makes it a very powerful mechanism. Here we have another class, Magento, Catalog, Model, Product, Reserved Attribute List dot php with the type list parameter product model. So what will the parameter be in this case? Once again, we need to take a look at the di dot xml file and search for product model.
Here, we see a declaration for the argument class, Magento, Catalog, Model, Product, Reserved Attribute List. This argument, product model, will deposit the string.
So here you can see where this string will be deposited as product model, and you can see it will do some work with that string. The next example is for arrays. Here is a similar example to the data array for product we saw earlier.
We have an array of reserved Attributes, an array of allowed Attributes. Again, they are empty, but by using the di dot xml file, for this or any other model, you can send something to these arrays. It is a very important practice in Magento Two and a very powerful mechanism for customization. Let’s look into this a little further. We have a second argument, reserved Attributes of type array, and it specifies an item. The item is also of type string and the item is positioned. We have a list of allowed attributes here in di dot xml, so, in reality, when an instance of this class is created, the string for product model, which is Magento, Catalog, Model, Product, will be set here. The array of reserved Attributes will now contain one item, position, and the array of allowed attributes will also not be empty, but will contain three items, type id, calculated final price, and request path. What is especially nice is that we can create our own model, and then, in the di dot xml, add another item to the array to customize it.
Hopefully, this demo has helped you to better understand how dependency injection works in Magento Two, and how we can use it for customization via the di dot xml file. -

Useful resources

recommendation-more-help
3a5f7e19-f383-4af8-8983-d01154c1402f