290 likes | 437 Views
Ibiza, June 4 th – 7 th 2011 . Magento Developers Paradise. Developing Loosely Coupled Modules with Magento. Sergey Shymko Magento 2 Team. Introduction and Terminology. Coupling or dependency is the degree to which each program module relies on each one of the other modules
E N D
Magento Developers Paradise Developing Loosely Coupled Modules with Magento Sergey Shymko Magento 2 Team
Introduction and Terminology • Coupling or dependency is the degree to which each program module relies on each one of the other modules • Coupling measures the likelihood of a change or fault in one module affecting another module
Module Purpose/Behavior • Module introduces new feature • Module injects into existing functionality • Module integrates with existing feature • Module extends/overlaps existing functionality
Magento Developers Paradise Modules Coupling WhileInjecting Into the Functionality
General Task of Injecting into Functionality • Module A exists and already implements the functionality • Module B expands the Module A • Module B should be triggered at the specific point of the Module A execution
Hard-Coded Call Implementation Framework • Module A highly relies on Module B • Module Bcan’t be removed Module A Module B Call
Conditional Call Implementation Framework • Module existence checking • Module B nowcan be removed • Module A still knows about Module B • Rename Module B – change Module A • Add new module – change Module A Module A Module B Call if (exists(‘Module B’)) { call(‘Module B’); } Module C
Maintaining the List of Dependents Framework • It’s observer pattern • Module Brelies on Module A • New modules can register within the Module A without changing anything • Rename Module A – change dependent modules Module A Module B Register Notify Module C Notify Register
Publish/Subscribe Messaging Pattern • Pub/sub – universal decoupling solution • senders of messages do not program the messages to be sent directly to specific receivers • subscribers express interest in messages without knowledge of publishers
Low Modules Coupling Using Pub/Sub Pattern Framework Messages Manager • Framework should take care of coupling • Modules don’t know about each other • Any module can be removed • New modules can subscribe to existing messages without changing anything Subscribe Subscribe Publish Module A Module B Call Module C Call
Magento Events • Magento events – object-oriented implementation of the pub/sub pattern • Event dispatching calls – points of integration • Subscription through config.xml
Low Modules Coupling with Magento Events Framework Config Manager Merged config.xml Events Manager Module A Module B Subscribe config.xml config.xml Call Mage::dispatchEvent
Achieved Coupling with Magento Events • Coupling depends on the implementation of the particular event handler
Possible Improvements in Magento Events • Strict event data types • Convention over configuration • Events naming convention
Strict Event Data Types • Own event class for each unique parameters combination • Formal data structure declaration • Access restriction to event data • Events documentation auto-generation
Convention over Configuration • Also known as coding by convention • Decreasing the number of decisions that developers need to make • Simplicity while keeping flexibility • Providing good defaults
Events Subscription in Configuration <events> <catalog_product_load_after> <observers> <inventory> <class>cataloginventory/observer</class> <method>addInventoryData</method> </inventory> </observers> </catalog_product_load_after> • Mapping events to handling routines • Declaration of handlersfor each area classMage_CatalogInventory_Model_Observer { public function addInventoryData($observer) { // ... }
No Events Subscription in Configuration <config> <global> <events/> </global> <frontend> <events/> </frontend> <adminhtml> <events/> </adminhtml> </config> • Good default mapping • Single observer class per module • Method name equals event name classMage_CatalogInventory_Model_Observer { public function catalog_product_load_after($observer) { // ... }
Event Naming Convention • lowerCamelCase event name to correspond to the method name
Magento Developers Paradise Modules Coupling While Integrating with Existing Feature
Sample Task – Cron Feature • Module Mage_Cron introduces time-based jobs scheduling feature • Any module should be able to schedule its jobs
High Modules Coupling Framework Mage_Cron My_Module Schedule Job Get Schedule & Run Job Removing Mage_Cron breaks My_Module
Magento Configuration Files • Configuration files “merging” from all enabled modules • Configuration allows integration with: • system-wide feature • feature provided by the module
Low Modules Coupling Using Configuration Framework Config Manager Merged config.xml Mage_Cron My_Module Get Schedule Schedule Job config.xml config.xml Run Job Removing Mage_Cron doesn’t break anything
Configuration Usage in Magento • Cache management • Index management • Product types • Translation files • Layout files • …
Magento Developers Paradise Summary
Recommendations • Follow the low coupling principles – use events to communicate between modules • Extend only when no events are triggered • Report requests for adding new events • Don’t hesitate to trigger events in your third-party modules and extensions
Magento Developers Paradise Thank You! Questions & Answers Sergey Shymko sergey.shymko@magento.com