Wednesday, July 15, 2020
Home > Beginners of Magento 2 > What is Proxies in Magento 2 And Benefits?

What is Proxies in Magento 2 And Benefits?

Proxies is a powerful and easy technique to help you overcome a huge problem of Magento 2. However, its role and importance are still underestimated. Therefore, in this post, I am going to help you to understand what is Magento 2 proxies and its need in Magento 2.

Just similar to design patterns which are created to solve a redundant problem in the project, proxy design pattern solves a particular problem. Proxies work as a surrogate which means it acts on behalf of others, in programming, they are classes which could be used instead of any other class. More specifically, in Magento 2, proxies are used to replace resource hungry classes.

However, when replacing like that, an issue happens in Magento 2

In Magento 2, dependency injection was introduced by a new concept. Although there are various types of dependency, constructor injection and method injection are the best, and both of them are used by Magento 2. However, there is a problem with constructor injection.

In details, if you want any other class object to be used in your class, instead of instantiating it inside the class, you should inject that object to the constructor and then use it in the class. If you do that, when your class is instantiated, all the dependencies which are injected in your class constructor will also be instantiated. As a result, a chain reaction of object creation will be triggered, which can lead to the process being slow down. Therefore, to prevent the object creation’s chain reaction, the proxy design pattern is used.

Dependency injection has influent on the solution for the issue which is mentioned above in some way. This is because the dependency injection configuration file ( di.xml) gives you the power to change the object lifestyle also known as constructor params. As a developer, you can understand which class is the culprit easily, and stop its instantiation in the constructor. In order to do that, add a type configuration in the di.xml

Spread the love

Leave a Reply

Your email address will not be published.