29
Click to edit Master title style Ibiza, June 4 th – 7 th 2011

Developing Loosely Coupled Modules with Magento

Embed Size (px)

Citation preview

Page 1: Developing Loosely Coupled Modules with Magento

Click to edit Master title style

Ibiza, June 4th – 7th 2011

Page 2: Developing Loosely Coupled Modules with Magento

Magento Developers Paradise

Developing Loosely Coupled Modules with Magento

Sergey ShymkoMagento 2 Team

Page 3: Developing Loosely Coupled Modules with Magento

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

Page 4: Developing Loosely Coupled Modules with Magento

Module Purpose/Behavior

• Module introduces new feature• Module injects into existing functionality• Module integrates with existing feature• Module extends/overlaps existing

functionality

Page 5: Developing Loosely Coupled Modules with Magento

Magento Developers Paradise

Modules Coupling WhileInjecting Into the Functionality

Page 6: Developing Loosely Coupled Modules with Magento

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

Page 7: Developing Loosely Coupled Modules with Magento

Hard-Coded Call Implementation

Framework

Module A Module B

Call

• Module A highly relies on Module B

• Module Bcan’t be removed

Page 8: Developing Loosely Coupled Modules with Magento

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

Page 9: Developing Loosely Coupled Modules with Magento

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

Page 10: Developing Loosely Coupled Modules with Magento

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

Page 11: Developing Loosely Coupled Modules with Magento

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

Page 12: Developing Loosely Coupled Modules with Magento

Magento Events

• Magento events – object-oriented implementation of the pub/sub pattern

• Event dispatching calls – points of integration

• Subscription through config.xml

Page 13: Developing Loosely Coupled Modules with Magento

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

Page 14: Developing Loosely Coupled Modules with Magento

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

Page 15: Developing Loosely Coupled Modules with Magento

Possible Improvements in Magento Events

• Strict event data types• Convention over configuration• Events naming convention

Page 16: Developing Loosely Coupled Modules with Magento

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

Page 17: Developing Loosely Coupled Modules with Magento

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

Page 18: Developing Loosely Coupled Modules with Magento

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) { // ... }

Page 19: Developing Loosely Coupled Modules with Magento

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) { // ... }

Page 20: Developing Loosely Coupled Modules with Magento

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

Page 21: Developing Loosely Coupled Modules with Magento

Magento Developers Paradise

Modules Coupling WhileIntegrating with Existing Feature

Page 22: Developing Loosely Coupled Modules with Magento

Sample Task – Cron Feature

• Module Mage_Cron introduces time-based jobs scheduling feature

• Any module should be able to schedule its jobs

Page 23: Developing Loosely Coupled Modules with Magento

Framework

High Modules Coupling

Mage_Cron

Removing Mage_Cron breaks My_Module

My_ModuleSchedule Job

Get Schedule & Run Job

Page 24: Developing Loosely Coupled Modules with Magento

Magento Configuration Files

• Configuration files “merging” from all enabled modules

• Configuration allows integration with:– system-wide feature – feature provided by the module

Page 25: Developing Loosely Coupled Modules with Magento

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

Page 26: Developing Loosely Coupled Modules with Magento

Configuration Usage in Magento

• Cache management• Index management• Product types• Translation files• Layout files• …

Page 27: Developing Loosely Coupled Modules with Magento

Magento Developers Paradise

Summary

Page 28: Developing Loosely Coupled Modules with Magento

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

Page 29: Developing Loosely Coupled Modules with Magento

Magento Developers Paradise

Thank You!

Sergey [email protected]

Questions & Answers