Skip to content
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

Inconsistent ways of accessing the registry #890

Closed
SchumacherFM opened this issue Jan 2, 2015 · 4 comments
Closed

Inconsistent ways of accessing the registry #890

SchumacherFM opened this issue Jan 2, 2015 · 4 comments

Comments

@SchumacherFM
Copy link
Member

During some review I've found three different ways of reading and writing to the registry:

$this->_registry->register(self::REGISTRY_KEY_CURRENT_INTEGRATION, $integrationData);
$this->_coreRegistry->register('current_widget_instance', $widgetInstance);
$this->_objectManager->get('Magento\Framework\Registry')->register('current_category', $category);

Which one is the official way?

@antonkril
Copy link
Contributor

Recommended way is to pass parameters to methods directly instead of using registry, but since registry is available the official way is №1.

№2 is the result of automated tool that used part of namespace to generate unique variable name. This option is fine too because It's totally OK to name variables referencing same services differently in different clients.

№3 is not recommended way because objectManager should not be referenced directly. I think this is the result of "quick" manual substitution of Mage::getSingleton

@EliasZ
Copy link
Contributor

EliasZ commented Jan 2, 2015

@antonkril

№3 is not recommended way because objectManager should not be referenced directly

Any argumentation? Is DI always preferred? When should the objectManager be used?

@antonkril
Copy link
Contributor

Usage of objectManager as service locator leads to highly coupled code and hidden dependencies. Also ability to configure specific arguments for the client that uses OM directly is lost.

Object manager can be used only in composition root of application (Bootstrap) and for unserialization of entities with service dependencies.

@SchumacherFM
Copy link
Member Author

Thank you anton that helped a lot!

magento-team pushed a commit that referenced this issue Mar 10, 2017
[SOUTH] Bugs:
- MAGETWO-64643 Product Image Import - Incorrect Position
- MAGETWO-64716 Product name ignores HTML tags and displays plain text
- MAGETWO-59821 Gift card totals are not displaying correctly
- MAGETWO-63200 State field is not required on checkout even though it is configured to be
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants