Upload
sergii-shymko
View
93
Download
0
Embed Size (px)
Citation preview
Click to edit Master title style
Ibiza, June 4th – 7th 2011
Magento Developers Paradise
Developing Loosely Coupled Modules with Magento
Sergey ShymkoMagento 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 Module B
Call
• Module A highly relies on Module B
• Module Bcan’t be removed
Conditional Call Implementation
Framework
Module A Module B
Call
• 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 Aif (exists(‘Module B’)) {
call(‘Module B’);}
Module C
Maintaining the List of Dependents
Framework
Module A Module B
Register
• It’s observer pattern• Module B
relies on Module A• New modules can
register within the Module A without changing anything
• Rename Module A – change dependent modulesModule C
Notify
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
Module A Module B
• 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 anythingModule C
Messages Manager
Publ
ish
Subs
crib
e
Call
Subs
crib
e
Call
Magento Events
• Magento events – object-oriented implementation of the pub/sub pattern
• Event dispatching calls – points of integration
• Subscription through config.xml
Framework
Low Modules Coupling with Magento Events
Module A
Subs
crib
e
Module B
Call
config.xml
Config Manager
config.xml
Merged config.xml
Events Manager
Mage::dispatchEvent
Achieved Coupling with Magento Events
Coupling depends on the implementation of the particular event handler
Coupling Type Description
Message coupling (low) Event doesn’t pass any data
Data coupling Simple event parameters, each is used
Stamp coupling Event data structure contains fields which may or may not be used
Common coupling Usage of the global data registry; Accessing data not only through methods
Content coupling (high) Not applicable to the pub/sub
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
• Mapping events to handling routines
• Declaration of handlersfor each area
<events> <catalog_product_load_after> <observers> <inventory> <class>cataloginventory/observer</class> <method>addInventoryData</method> </inventory> </observers> </catalog_product_load_after>
class Mage_CatalogInventory_Model_Observer{ public function addInventoryData($observer) { // ... }
No Events Subscription in Configuration
• Good default mapping• Single observer class
per module• Method name equals
event name
<config><global>
<events/></global><frontend>
<events/></frontend><adminhtml>
<events/></adminhtml>
</config>
class Mage_CatalogInventory_Model_Observer{ public function catalog_product_load_after($observer) { // ... }
Event Naming Convention
Event Name Part Description
1. Module Name of the module, which introduces an event.
2. Layer Application layer where an event appears.Allowed values: model, view, controller, service.
3. Entity Entity (domain) to which event has relation.
4. Action By default action should be equal to the method name, which triggers an event.
[5. Suffix] Optional. Allowed values: before, after
lowerCamelCase event name to correspond to the method name
Magento Developers Paradise
Modules Coupling WhileIntegrating 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
Framework
High Modules Coupling
Mage_Cron
Removing Mage_Cron breaks My_Module
My_ModuleSchedule Job
Get Schedule & Run Job
Magento Configuration Files
• Configuration files “merging” from all enabled modules
• Configuration allows integration with:– system-wide feature – feature provided by the module
Framework
Low Modules Coupling Using Configuration
Mage_Cron
config.xml
My_Module
config.xmlGet S
ched
ule
Run Job
Removing Mage_Cron doesn’t break anything
Config ManagerMerged config.xml
Sche
dule
Job
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