Magento, Magento 2

Proxies In Magento 2

Proxies in Magento 2

Hello Everyone, welcome to my blog. Today I am going to discuss the proxy design pattern and how it is used in Magento 2.

What is Proxy Pattern?

A proxy is a structural design pattern that lets you provide a substitute or placeholder for another object. A proxy controls access to the original object, allowing you to perform something either before or after the request gets through to the original object.

Why we need a Proxy?

For example, you have a resource-intensive object, an object which is consuming more resource and affect the performance and we don’t even need that object every time. To tackle this situation you can use lazy loading of that resource-intensive class. 

How does Proxy work?

To improve the performance we can use Proxy classes. So, how does it work? We need to create an interface that is a substitute for the original class object. If you need to execute something either before or after the primary logic of the class, the proxy lets you do this without changing that class. Since the proxy implements the same interface as the original class, it can be passed to any client that expects a real service object.

How does Proxy work in Magento?

We are using dependency injection to manage all our dependencies in Magento 2, let’s say you have a class A & that class A is having dependencies like B, C, D & B class is also having few more dependencies, so Magento’s centralized object creation process try to create an instance of all the dependency and it creates a chain reaction of object instantiation process. If the object particularly resources intensive, this can lead to unnecessary performance impact when another class depends on it. If the resource-intensive object is not needed in our code then it will impact the site performance.

In this case, proxy patterns come in. Proxy classes are also known as lazy loading classes because the actual call will not be made until it requires. Consider the following example.

class SlowLoading
{
    public function __construct()
    {
        // ... Do something resource intensive
    }

    public function getValue()
    {
        return 'SlowLoading value';
    }
}
class FastLoading
{
    protected $slowLoading;

    public function __construct(
        SlowLoading $slowLoading
    ){
        $this->slowLoading = $slowLoading;
    }

    public function getFastValue()
    {
        return 'FastLoading value';
    }

    public function getSlowValue()
    {
        return $this->slowLoading->getValue();
    }
}

Let’s assume that a slow loading class constructor calling a third-party API or doing any database transaction which will slow the performance. And, we are creating an object of slow loading class in Fast Loading class constructor, and we need a slow loading object when the getSlowValue() function gets called. If we don’t need the getSlowValue() function in Our code then it will unnecessarily load class objects and perform those heavy transactions. Magento has a solution on this proxy. We can create a proxy of the Slow Loading class and call the main class whenever it is required, i.e. When we call any function from a slow-loading class, Magento creates an object of the main class and calls its functions. 

        Proxies are generated code and therefore do not need to be manually written. Simply reference a class in the form \Original\Class\Name\Proxy, and the class is generated if it does not exist.

Let’s take a looks at another Proxy class Magento\Catalog\Model\Config\Proxy

This is an autogenerated class created for Magento\Catalog\Model\Config class. The proxy class extends the main class(Config) and implement NoninterceptableInterface. The proxy class overrides all the methods present in the main class and calls the functions through the requested object.

Proxies in Magento 2

This is all about the proxy pattern used in Magento 2.

Please let me know your thought about this blog in the comment section below. Or, you can write me an email on my email id i.e. admin@asolutions.co.in. Till next time happy coding :). 

[post-views]

Tagged , ,

Leave a Reply

Your email address will not be published. Required fields are marked *


The reCAPTCHA verification period has expired. Please reload the page.