AD0-E717 practice question 1 of 15

A developer is creating a class \Vendor\Module\Model\MyModeL How should that class be defined as transient in di.xml?

Full set: 77 questions
Answer options
A:

A.<type name="\Vendor\Module\Model\MyModer transient="true7>

B:

B. <type name="\Vendor\Module\Model\MyModer singleton='false7>

C:

C. <type name='\Vendor\Module\Model\MyModer shared="false7>

Correct answer
Correct Answer:
C. C. <type name='\Vendor\Module\Model\MyModer shared="false7>
Explanation

In Magento's dependency injection (DI) framework, an object's lifecycle is controlled by its configuration in di.xml. By default, objects are "shared" (singletons), meaning the object manager creates only one instance per request. To make an object "transient," which means a new instance is created every time it is requested, you must explicitly configure it as non-shared. The correct syntax for this is setting the shared attribute of the node to false. This instructs the DI container not to share the instance, effectively making it transient.

Why Incorrect

A. The transient attribute is not a valid attribute for the node in the di.xml schema.
B. The singleton attribute is not a valid attribute for the node in the di.xml schema. While the concept is correct, the syntax is not.

References

1. Adobe Commerce Developer Documentation, Configure object types:

Section: The element

Quote: "The shared attribute is a boolean that determines if a single instance of the object is created. If shared is false, the object manager creates a new instance of the object every time it is requested. This is also known as a transient object. The default value is true."

Source: Adobe Commerce Developer Documentation, "Configure object types", https://developer.adobe.com/commerce/php/development/build/dependency-injection-file/ (Accessed under the "The element" section).

2. Adobe Commerce Developer Documentation, Technical Bulletin: Validate your XML:

Section: di.xml

Reference: This document points to the official URN for the di.xml schema (urn:magento:framework:ObjectManager/etc/config.xsd). Analyzing this schema file confirms that for the type element, shared is a valid attribute, while transient and singleton are not.

Source: Adobe Commerce Developer Documentation, "Technical Bulletin: Validate your XML", https://developer.adobe.com/commerce/php/development/build/validate-xml/

Scroll to Top