Virtual Type and Type

Blog
Spread the love

Virtual type is one of the most common design patterns in magento2. Basically, it allows us to modify existing classes without affecting other classes and inject them where we need them

Type allows us to modify dependencies injected to a constructor.

Consider following chunk of code:

We basically pass an object of our Bluethink\Crud\Model\ResourceModel\Post\Grid\Collection class to the Magento\Framework\View\Element\UiComponent\DataProvider\CollectionFactory constructor of the bluethink_post_listing_data_source variable.

But type isn’t just about objects. It can be any value (e.g. array) that can be passed to the constructor.

has two attributes name and type, type is an existing class and name is virtual class (It does not need to create). Virtual types also can be used to manipulate the constructor argument of an existing class.

Consider following chunk of code:

The above code is almost same as creating a PHP class Grid:

Here is another chunk of code:

In the above example, We are modifying the constructor argument mainTable and resourceModel of class Magento\Framework\View\Element\UiComponent\DataProvider\SearchResult

Consider you want to make a coffee for a party and each guest is told to bring a cup of coffee.

First, use the Espresso class in your code, where you’ll throw the coffee cups’ contents.

Now, you would put all coffee flavors into the Cup class:

Now collect the flavor cup and then move them into the Espresso. Let’s assume you first create 2 cups; one with Amaretto & chocolate and another with Hazelnut & Almond.

Next, we have our cups ready, let’s move them inside Espresso:

Conclusion: Now the cups are now in the espresso, and you are ready to make coffee 😉

Vikas Mishra

Vikas Mishra

2024-02-06

0

Leave a Reply

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

Find More Blogs

Custom API to Fetch Customer Data by Customer ID in Spryker

Spread the love Introduction In this blog post, we will

Creating a Custom Module for a Custom Frontend Page in Spryker

Spread the love To create a custom module for a custom

How to create custom module api get category list without access token in Spryker

Spread the love To create a custom API for fetching a

Creating a Custom Glue API in Spryker

Spread the love Spryker’s Glue API is a powerful tool

Get AI Generated 360-Degree View of Account Summary in Dynamics 365 Sales

Spread the love Navigating the demanding landscape of sales

Enhancing Productivity with Dynamics 365: The Power of Session Restore

Spread the love Enhancing Productivity with Dynamics 365:

Salesforce Admin

Spread the love In our last blog, we discussed common

Unlocking AI with Spring Boot: Real-World Use Cases and Solutions

Spread the love Artificial Intelligence (AI) has moved from

Enhancing Performance with Reactive Java: A Beginner-Friendly Guide

Spread the love In today’s fast-paced software

Integrating Azure Function with Dynamics 365

Spread the love Signup on azure portal and make sure to

bluethinkinc Blogs