Ibiza, June 4
This presentation is the property of its rightful owner.
Sponsored Links
1 / 29

Ibiza, June 4 th – 7 th 2011 PowerPoint PPT Presentation


  • 44 Views
  • Uploaded on
  • Presentation posted in: General

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

Download Presentation

Ibiza, June 4 th – 7 th 2011

An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Presentation Transcript


Ibiza june 4 th 7 th 2011

Ibiza, June 4th – 7th2011


Ibiza june 4 th 7 th 2011

Magento Developers Paradise

Developing Loosely Coupled Modules with Magento

Sergey Shymko

Magento 2 Team


Ibiza june 4 th 7 th 2011

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


Ibiza june 4 th 7 th 2011

Module Purpose/Behavior

  • Module introduces new feature

  • Module injects into existing functionality

  • Module integrates with existing feature

  • Module extends/overlaps existing functionality


Ibiza june 4 th 7 th 2011

Magento Developers Paradise

Modules Coupling WhileInjecting Into the Functionality


Ibiza june 4 th 7 th 2011

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


Ibiza june 4 th 7 th 2011

Hard-Coded Call Implementation

Framework

  • Module A highly relies on Module B

  • Module Bcan’t be removed

Module A

Module B

Call


Ibiza june 4 th 7 th 2011

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


Ibiza june 4 th 7 th 2011

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


Ibiza june 4 th 7 th 2011

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


Ibiza june 4 th 7 th 2011

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


Ibiza june 4 th 7 th 2011

Magento Events

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

  • Event dispatching calls – points of integration

  • Subscription through config.xml


Ibiza june 4 th 7 th 2011

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


Ibiza june 4 th 7 th 2011

Achieved Coupling with Magento Events

  • Coupling depends on the implementation of the particular event handler


Ibiza june 4 th 7 th 2011

Possible Improvements in Magento Events

  • Strict event data types

  • Convention over configuration

  • Events naming convention


Ibiza june 4 th 7 th 2011

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


Ibiza june 4 th 7 th 2011

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


Ibiza june 4 th 7 th 2011

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)

{

// ...

}


Ibiza june 4 th 7 th 2011

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)

{

// ...

}


Ibiza june 4 th 7 th 2011

Event Naming Convention

  • lowerCamelCase event name to correspond to the method name


Ibiza june 4 th 7 th 2011

Magento Developers Paradise

Modules Coupling While

Integrating with Existing Feature


Ibiza june 4 th 7 th 2011

Sample Task – Cron Feature

  • Module Mage_Cron introduces time-based jobs scheduling feature

  • Any module should be able to schedule its jobs


Ibiza june 4 th 7 th 2011

High Modules Coupling

Framework

Mage_Cron

My_Module

Schedule Job

Get Schedule & Run Job

Removing Mage_Cron breaks My_Module


Ibiza june 4 th 7 th 2011

Magento Configuration Files

  • Configuration files “merging” from all enabled modules

  • Configuration allows integration with:

    • system-wide feature

    • feature provided by the module


Ibiza june 4 th 7 th 2011

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


Ibiza june 4 th 7 th 2011

Configuration Usage in Magento

  • Cache management

  • Index management

  • Product types

  • Translation files

  • Layout files


Ibiza june 4 th 7 th 2011

Magento Developers Paradise

Summary


Ibiza june 4 th 7 th 2011

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


Ibiza june 4 th 7 th 2011

Magento Developers Paradise

Thank You!

Questions & Answers

Sergey Shymko

[email protected]


  • Login