I am learning the Magento 2 proxy concept so that I followed the https://webkul.com/blog/magento2-proxy-design-pattern-code-generation/ blog and discovered the proxy concept.
I am a bit clear about the proxy theory, But I want to implement in real-time so that I followed the below procedure to learn the practical.
Model:
namespace Learn\Pricechange\Model;
class BulkService {
public function __construct() {
echo 'i am in bulk service instantiated'."<br/>";
}
}
I am going to use this as a proxy in my controller.
Controller:
namespace Learn\Pricechange\Controller\Index;
use Learn\Pricechange\Model\BulkService;
class Change extends \Magento\Framework\App\Action\Action
{
protected $_bulkService;
public function __construct(
\Magento\Framework\App\Action\Context $context,
BulkService $bulkService,
array $data = []
) {
$this->_bulkService = $bulkService;
parent::__construct($context);
}
public function execute(){
echo 'bulkservice class is not instantiated';
}
}
Then as per the proxy concept, I included the required code in frontend/di.xml
di.xml
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<type name="Learn\Pricechange\Controller\Index\Change">
<arguments>
<argument name="cart" xsi:type="object">Magento\Checkout\Model\Cart\Proxy</argument>
<argument name="bulkservice" xsi:type="object">Learn\Pricechange\Model\BulkService\Proxy</argument>
</arguments>
</type>
</config>
so as per the above code when you hit the frontend, it should not instantiate the BulkService and do not print the constructor function value(i am in bulk service instantiated).
But in my case it's getting instantiated first and print the constructor function values(i am in bulk service instantiated) after that it's print the execute function value.
So i am bit confused this logic, I am looking forward the better support.