Redeem Now
Read More
Read now
Menu Close
    Start a Project Request quote Reading list Switch to dark mode
    magento-hyperlocal-marketplace-guide

    Magento2 Proxy Design Pattern And Code Generation

    This is the last blog on code generation, in my previous articles we understood factory and interceptor code generation, in this blog we will try to understand magento2 proxy design pattern and code generation and its need in magento2.

    What is proxy design pattern :

    As we know design pattrens are meant for solving a redundant problem in the whole project , proxy design pattern also solves a specific problem. Proxies works as surrogate(means to act on behalf of others) , in programming proxies are classes that can be used in place of any other class, and in magento2 proxies are used in place resource hungry classes.

    Lets understand why this problem comes in magento2.

    Problem:

    In magento2 a new concept introduced dependency injection, there are many types of dependency injection but the best one is constructor injection and method injection and magento2 uses both of them, but there is problem with constructor injection, constructor injection says if you want to use any other class object in your class, don’t instantiate it inside the class, inject the object to the constructor and then use the object in the class, so when your class will be instantiated all the dependencies injected in your class constructor will also get instantiated, and it will trigger a chain reaction of object creation, this can really slow down the process, so to stop chiain reaction of object creation magento2 uses proxy design pattern.

    Solution:

    Solution of this problem also depends somehow on dependency injection, Since the dependency injection configuration file(di.xml) gives you the control that you can change the object life style(constructor params). So as a developer you can easily understand which class is the culprit, and you need to stop its instantiation in the constructor, So you need to simply add a type configuration in di.xml like below:

     


    the above entry is from catalog module di.xml, as we can see in this type declaration for class “Magento\Catalog\Model\Product” , it replaces constructor argumets catalogProductStatus and productLink with Magento\Catalog\Model\Product\Attribute\Source\Status\Proxy and “Magento\Catalog\Model\Product\Link\Proxy” as you can see both these classes have name “Proxy”, if you will check your magento after installing it, you will not be able to found these proxy classes in magento2 code base, these proxy classes are generated automatically by magento2 code generator. These classes are genrated in two circumstances :

    •  when you will execute the magento cli command :

      when you will execute the above command, it will check all the di.xml files of all the modules installed in magento2 and if it encounters a class named Proxy, and it does not exist, then it will generate it automatically with same namespace inside ver/generation folder in the magento2 root .
    •  In this case when a class is requested and it is a Proxy class and it is not present in the current code base magento will not throw an error or exception it will simply create that class inside var/generation folder and retun its object .

    So you can see that magento2 generate these proxy classes on the fly with some fixed convention and replaces the original object with proxy, but we still don’t know how this proxy class is going to solve the problem, we can understand this by simply analysing a proxy class, lets have a look on the below proxy class:

     


    Lets understand the proxy class:

    •  the namespace of the proxy class is same as the original class “Magento\Catalog\Model\Product\Attribute\Source\Status” so that it can be easily replaced with the original object .
    •  Proxy class extends the original class “\Magento\Catalog\Model\Product\Attribute\Source\Status” so that it can use its public methods.
    •  Proxy classes implements “\Magento\Framework\ObjectManager\NoninterceptableInterface” interface, this is a marker interface menas it is blank, it has no methods and no member variables, it for only signaling that this class clannot be intercepted .
    • And the most important its constructor, as we can see it only has one object dependency that ObjectManager class, so this makes sense as there is only one dependency in the class it will load very quickly and will save a lot of resources(space and time), you will not see a parent constructor call too .
    •  __sleep, __wakeup and __sleep these are the php magic functions that executes when a class is invoked or destroyed to perform some actions.
    • _getSubject this method will return the original class object whenever it will be asked for, so it will simply work like a lazy loding process an object will be only provided when it will be requested.
    • And all the other functions are parent(original) class public functions that are overriden in the proxy class so that they can be called on proxy class object, inside these functions it is calling the _getSubject to get the parent object and then call the same function .

    So now I hope you understand how powerfull and easy technique is this to overcome a potentially huge problem .

    There are some of the benifits of using proxy classes:

    •  Easy to use, you don’t need to do any thing just a simple entry in di.xml file will solve the problem.
    •  Performance of the application improves, since it saves a lot of time.
    •  No need to write test cases for the generated classes.
    •  Easy to understand the structure, since all the proxies will have the same structure .

    Thats all in this blog, I hope it is easy to understood, but if you have any questions regarding this blog you can ask me in the comments below, I will surely try to answer your questions.

    Thanks 🙂 .

    . . .

    Leave a Comment

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


    10 comments

  • Mehul
    Great artical…!! However as you mentioned that generated proxy class will have all public methods of class that we have declared/used as proxy. So, what would happen if that class contains private methods or properties. would we able use the same by this proxy injected object ?
    • ashutosh srivastava (Moderator)
      Proxy is supposed to replace the object in the constructor, and in any object scope you are not allowed to access private or protected methods, so what you can do with original class object you can do the same with proxy class object as well.
      • Mehul
        Thanks for quick reply…!!. You made correct point as using DI we couldn’t able to access private method of that class any way. So, using proxy is completely fine for that either
  • ravi
    I have one question here lets say we have one class if that class extends proxy class then what will happen??

    class Example extends Crazy\SimpleSolution\Model\Test\Proxy

    if this possible ?? if not why

    • ashutosh srivastava (Moderator)
      yes it will work.
  • Archana B.
    In magento 2.3 we dont even need to mention it in di.xml, we can simply call the file with proxy suffixed on it thats what i have observed in one of the module,right?
    • ashutosh srivastava (Moderator)
      Please give an example where you have seen this, proxies always injected in the constructor for replacing resource-hungry objects
  • Hiren Kava
    Please confirmed that i understood what you have tried to guide here.
    there is abc class and i need to call some function from xyz class.
    Normal practice is xyz add in constructor and make a variable in constructor and use it. so xyz also will load while initialize abc object. but with the help of proxy xyz class will not load while initialize abc object and although we can use xyz class function in abc class if we define xyz class as proxy in di.xml. am i right?
    • ashutosh srivastava (Moderator)
      Yes, this is exactly what is happening, it’s not like that xyz class is not loading, you can see the proxy class it removes everything from the constructor which makes the object load faster.
  • Anonymous
    I know this is a pretty old article, but your design patterns series are so useful if you just want to understand what is happening in your Magento 2 project. Thanks!
  • Back to Top
    css.php
    Hire Us!
    Brief us about your requirements and we'll get back to you.
    Woo! Hooy!
    We have just recieved your project brief and our expert will contact you shortly.
    Send Again
    Close