-
Notifications
You must be signed in to change notification settings - Fork 9.4k
Deprecate result factories in favor of one generic #24711
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Deprecate result factories in favor of one generic #24711
Conversation
Hi @ihor-sviziev. Thank you for your contribution
For more details, please, review the Magento Contributor Guide documentation. |
Hi @VladimirZaets, thank you for the review. |
For now moved to this PR "on hold" till it be discussed with Magento Architects |
@ihor-sviziev are we even able to add new deprecations within |
https://testing-service.magento-community.engineering/reports/magento/magento2/pull/24711/7e54f79e68bbc21bc69ccadb41cd037a/SemanticVersionChecker/report-magento2.html LOL :) So naive test implementation. I suppose it is even based on regular expressions. |
Summary of Arch discussion around this topic:
Based on this, we all agreed that it's better to use specialized factories than generic one. And from the three described variants, 1st one (factory injected in constructor) should be a guideline. |
So in this case should we deprecate generic ResultFactory? |
Great @buskamuza!
Missed this when we discussed in Slack, really nice catch 👍 |
Actually not, in many places we'll have two types of result - Page & Forward. Example We could apply this example to any entity on frontend, result will be the same. I see your points, but still don't think that introducing multiple dependencies instead of one - good idea. Am I missed something?
Sure, this is good point. Maybe we need to introduce new result factory that will have methods "createPageResult", "createRedirectResult"...? Actually currently we have 2 types of results - frontend & backend, they have the same interface |
Hi @ihor-sviziev, thank you for your contribution! |
Yes, we have such places.
I think this topic is partially subjective. As for me, having two factories injected is a lesser issue than having a single factory with a non-strict interface. So I'd prefer having multiple factories. Especially in action controllers, which are (or should be) small and having one more dependency should not be a big issue. In the same time it would clearly show the dependencies used. |
@buskamuza ok, so another question. If using "pageFactory" is not really good option - maybe we need to mark this function as deprecated with description why and alternatives? Why I started thinking about it - because another people creates PRs that uses this approach, example: |
Description (*)
Issue
In magento we have a mess with result objects in adminhtml controllers. When you want to add new result type, for instance add json result - you have three options
Inject new type with as constructor argument
magento2/app/code/Magento/Cms/Controller/Adminhtml/Wysiwyg/Images/Upload.php
Line 39 in 20069c4
magento2/app/code/Magento/Cms/Controller/Adminhtml/Wysiwyg/Images/Upload.php
Line 65 in 20069c4
Inject it via ObjectManger (really-really bad, but we could do that):
magento2/app/code/Magento/Catalog/Controller/Adminhtml/Category/Add.php
Line 62 in 20069c4
magento2/app/code/Magento/Catalog/Controller/Adminhtml/Category/Add.php
Line 64 in 20069c4
Use already existing
resultFactory
object:magento2/lib/internal/Magento/Framework/App/Action/AbstractAction.php
Line 44 in 20069c4
magento2/app/code/Magento/AdminNotification/Controller/Adminhtml/System/Message/ListAction.php
Line 72 in 20069c4
Examples
In many places we're using 1st option that for different types causes not needed code duplicates, examples:
magento2/app/code/Magento/Sales/Controller/Adminhtml/Order.php
Lines 111 to 114 in 20069c4
magento2/app/code/Magento/Checkout/Controller/Onepage.php
Lines 114 to 117 in 20069c4
magento2/app/code/Magento/Customer/Controller/Adminhtml/Index.php
Lines 222 to 225 in 20069c4
Solution
My PR marks all specific result factories as
@deprecated
in favor of one generic factory that will have the same result, so we'll have only one option how to create result objects. It should decrease amount of mess in controllers.Fixed Issues (if relevant)
N/A
Manual testing scenarios (*)
N/A
Questions or comments
I checked - option 3 works correctly in admin because it replaces result object types:
magento2/app/code/Magento/Backend/etc/adminhtml/di.xml
Lines 97 to 114 in 20069c4
Contribution checklist (*)