SlideShare uma empresa Scribd logo
1 de 23
Baixar para ler offline
Magento 2
The State of Data Persistence
(Being Pragmatic in the Face of Architectural Uncertainty)
@matthewhaworth
A quick about me
Matthew Haworth
Magento Consultant at Warner Music Group
Twitter: @matthewhaworth
LinkedIn: matthewhaworth
@matthewhaworth
My first experience with Magento 2.0
• Stayed in my comfort zone using old ‘Active Record’ façade
$product = $productFactory->create()->load($productId);
$product->setName(‘New Product Name');
$product->save();
• Learned about new ‘Service Contracts’ architecture, but avoided it
• Eventually dug deeper, learned more best practice and want to
evangelise
@matthewhaworth
Magento 1 Architecture Review
• The Active Record pattern implied that a Model was responsible for
persisting data to the database through ->save()
• However, the Model delegated to the Resource Model
• The Resource Model iterated through the database table’s fields and
pulled those keys from the Model to create an insert/update query.
@matthewhaworth
Model
Resource
Model
->save() ->save($this)
Magento 1 Architecture Review
• The Model was responsible for modelling the data.
• The Resource Model was effectively a Data Mapper.
• Any business logic was either in the Model or in a custom
built service class
@matthewhaworth
Magento 2’s New Architecture
• Magento 2 seeks to formalise this structure by introducing ‘Service
Contracts’.
• Service contracts are a set of interfaces defined for data models and
services.
@matthewhaworth
@matthewhaworth
Magento 2’s New Architecture
• Data persistence within service contracts starts with the ’repository’.
• “A Repository mediates between the domain and data mapping
layers, acting like an in-memory domain object collection.” – Martin
Fowler
@matthewhaworth
No more abstract model!
• Domain objects are now completely independent from the database!
@matthewhaworth
Model Abstract
Model
extends
@matthewhaworth
Magento 1
Magento 2
Model Abstract
Model
Resource Model
Abstract
Resource Model
extends extends
delegates database
map and save
Repository Interface
Repository
Implementation
implements
Data Interface
Data Object
Implementation
implements
is passed to
repository for
save
@matthewhaworth
Magento 1
Magento 2
Model Abstract
Model
Resource Model
Abstract
Resource Model
extends extends
delegates database
map and save
Resource Model
Abstract
Resource Model
extends
Repository Interface
Repository
Implementation
implements
Data Interface
Data Object
Implementation
implements
delegates
database map
and save
is passed to
repository for
save
Why move to repositories?
• Separate the representation of data, business logic and data
persistence
• Beneficial for testing by allowing a substitution point for unit tests
• Repository interfaces provides a version-able, single point of
communication for APIs and other modular dependencies
@matthewhaworth
But wait, there’s a problem…
The object passed to the resource must extend the Abstract Model
public function save(MagentoFrameworkModelAbstractModel $object)
@matthewhaworth
Resource ModelRepository InterfaceData Interface
So what are our options?
1. Implement data interface and extend the Abstract Model,
sacrificing our new found independence.
@matthewhaworth
Data Object
Abstract ModelData Interface
implements extends
So what are our options?
2. Implement the data interface and map to the Abstract Model in
the repository before passing to the resource.
@matthewhaworth
Abstract Model
Implementation
Data Object Repository Resource
map
So what are our options?
3. Best of both worlds!
Implement the data interface, the abstract model and map to the
Abstract Model in the repository before passing to the resource.
@matthewhaworth
Model & Data Object Repository Resource
2. Map to data object
Abstract ModelData Interface
implements extends
1. Submit to save
Model & Data Object
So what are our options?
4. Use a different data persistence layer to the Magento DB Resource,
maybe Doctrine?
@matthewhaworth
So what are our options?
4. Use a different data persistence layer to the Magento DB Resource,
maybe Doctrine?
@matthewhaworth
Nah.
For me, the clear winner is approach 3
• Implement the data interface and
extend the abstract model
• Map from the data interface to
the abstract model in the
repository
• Avoids duplicating an extra model
for the sake of pushing to the
resource
@matthewhaworth
Model & Data Object Repository
Resource
2. Map to data object
Abstract Model Data Interface
implementsextends
1. Submit to save
Model & Data Object
@matthewhaworth
by @VinaiKopp
Where can I see good examples?
Magento_Customer was regarded as the best example.
This module maps its data interfaces to a separate Abstract Model
implementation in its repository (approach 2).
Multi Source Inventory (a set of modules) is currently regarded as the
best example to follow.
Data models in this module both implement the data interface and
extend the data model (approach 3) and also make use of CQRS.
(see MagentoInventoryModelStock @ github.com/magento-engcom/msi)
@matthewhaworth
What does the future look like?
• Removing the Abstract Model entirely
• Moving beforeSave(), afterSave(), etc to plugins and events (AOP)
(They’re also making them ‘bulk-first’.)
• Command Query Responsibility Segregation
@matthewhaworth
Thank you to Alan Kent, Anton Kril and Vinai Kopp
@matthewhaworth
Questions?

Mais conteúdo relacionado

Semelhante a The State Of Data Persistence

Generic Repository Pattern in MVC3 Application with Entity Framework
Generic Repository Pattern in MVC3 Application with Entity FrameworkGeneric Repository Pattern in MVC3 Application with Entity Framework
Generic Repository Pattern in MVC3 Application with Entity Framework
Akhil Mittal
 
What is struts_en
What is struts_enWhat is struts_en
What is struts_en
techbed
 
Struts 2-overview2
Struts 2-overview2Struts 2-overview2
Struts 2-overview2
divzi1913
 

Semelhante a The State Of Data Persistence (20)

Generic Repository Pattern in MVC3 Application with Entity Framework
Generic Repository Pattern in MVC3 Application with Entity FrameworkGeneric Repository Pattern in MVC3 Application with Entity Framework
Generic Repository Pattern in MVC3 Application with Entity Framework
 
Design Patterns
Design PatternsDesign Patterns
Design Patterns
 
Struts Ppt 1
Struts Ppt 1Struts Ppt 1
Struts Ppt 1
 
Software Design Patterns
Software Design PatternsSoftware Design Patterns
Software Design Patterns
 
Software Design Patterns
Software Design PatternsSoftware Design Patterns
Software Design Patterns
 
Madison PHP - Getting Started with Magento 2
Madison PHP - Getting Started with Magento 2Madison PHP - Getting Started with Magento 2
Madison PHP - Getting Started with Magento 2
 
CoreData
CoreDataCoreData
CoreData
 
java framwork for HIBERNATE FRAMEWORK.pptx
java framwork for HIBERNATE FRAMEWORK.pptxjava framwork for HIBERNATE FRAMEWORK.pptx
java framwork for HIBERNATE FRAMEWORK.pptx
 
Build Java Web Application Using Apache Struts
Build Java Web Application Using Apache Struts Build Java Web Application Using Apache Struts
Build Java Web Application Using Apache Struts
 
What is struts_en
What is struts_enWhat is struts_en
What is struts_en
 
Struts 2-overview2
Struts 2-overview2Struts 2-overview2
Struts 2-overview2
 
common design patterns summary.pdf
common design patterns summary.pdfcommon design patterns summary.pdf
common design patterns summary.pdf
 
Server side programming bt0083
Server side programming bt0083Server side programming bt0083
Server side programming bt0083
 
Architecting an ASP.NET MVC Solution
Architecting an ASP.NET MVC SolutionArchitecting an ASP.NET MVC Solution
Architecting an ASP.NET MVC Solution
 
TY.BSc.IT Java QB U6
TY.BSc.IT Java QB U6TY.BSc.IT Java QB U6
TY.BSc.IT Java QB U6
 
Spring data presentation
Spring data presentationSpring data presentation
Spring data presentation
 
The Meteor Framework
The Meteor FrameworkThe Meteor Framework
The Meteor Framework
 
Having Fun Building Web Applications (Day 2 slides)
Having Fun Building Web Applications (Day 2 slides)Having Fun Building Web Applications (Day 2 slides)
Having Fun Building Web Applications (Day 2 slides)
 
MidwestPHP - Getting Started with Magento 2
MidwestPHP - Getting Started with Magento 2MidwestPHP - Getting Started with Magento 2
MidwestPHP - Getting Started with Magento 2
 
Reactive data analysis with vert.x
Reactive data analysis with vert.xReactive data analysis with vert.x
Reactive data analysis with vert.x
 

Último

%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
masabamasaba
 
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICECHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
masabamasaba
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service provider
mohitmore19
 

Último (20)

Chinsurah Escorts ☎️8617697112 Starting From 5K to 15K High Profile Escorts ...
Chinsurah Escorts ☎️8617697112  Starting From 5K to 15K High Profile Escorts ...Chinsurah Escorts ☎️8617697112  Starting From 5K to 15K High Profile Escorts ...
Chinsurah Escorts ☎️8617697112 Starting From 5K to 15K High Profile Escorts ...
 
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
 
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park %in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
 
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
 
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
 
Introducing Microsoft’s new Enterprise Work Management (EWM) Solution
Introducing Microsoft’s new Enterprise Work Management (EWM) SolutionIntroducing Microsoft’s new Enterprise Work Management (EWM) Solution
Introducing Microsoft’s new Enterprise Work Management (EWM) Solution
 
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
 
%+27788225528 love spells in Vancouver Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Vancouver Psychic Readings, Attraction spells,Br...%+27788225528 love spells in Vancouver Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Vancouver Psychic Readings, Attraction spells,Br...
 
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICECHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
 
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
 
AI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplateAI & Machine Learning Presentation Template
AI & Machine Learning Presentation Template
 
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
 
%in Durban+277-882-255-28 abortion pills for sale in Durban
%in Durban+277-882-255-28 abortion pills for sale in Durban%in Durban+277-882-255-28 abortion pills for sale in Durban
%in Durban+277-882-255-28 abortion pills for sale in Durban
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service provider
 
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
Direct Style Effect Systems -The Print[A] Example- A Comprehension AidDirect Style Effect Systems -The Print[A] Example- A Comprehension Aid
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
 
Architecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the pastArchitecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the past
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
 
SHRMPro HRMS Software Solutions Presentation
SHRMPro HRMS Software Solutions PresentationSHRMPro HRMS Software Solutions Presentation
SHRMPro HRMS Software Solutions Presentation
 
Announcing Codolex 2.0 from GDK Software
Announcing Codolex 2.0 from GDK SoftwareAnnouncing Codolex 2.0 from GDK Software
Announcing Codolex 2.0 from GDK Software
 
Exploring the Best Video Editing App.pdf
Exploring the Best Video Editing App.pdfExploring the Best Video Editing App.pdf
Exploring the Best Video Editing App.pdf
 

The State Of Data Persistence

  • 1. Magento 2 The State of Data Persistence (Being Pragmatic in the Face of Architectural Uncertainty) @matthewhaworth
  • 2. A quick about me Matthew Haworth Magento Consultant at Warner Music Group Twitter: @matthewhaworth LinkedIn: matthewhaworth @matthewhaworth
  • 3. My first experience with Magento 2.0 • Stayed in my comfort zone using old ‘Active Record’ façade $product = $productFactory->create()->load($productId); $product->setName(‘New Product Name'); $product->save(); • Learned about new ‘Service Contracts’ architecture, but avoided it • Eventually dug deeper, learned more best practice and want to evangelise @matthewhaworth
  • 4. Magento 1 Architecture Review • The Active Record pattern implied that a Model was responsible for persisting data to the database through ->save() • However, the Model delegated to the Resource Model • The Resource Model iterated through the database table’s fields and pulled those keys from the Model to create an insert/update query. @matthewhaworth Model Resource Model ->save() ->save($this)
  • 5. Magento 1 Architecture Review • The Model was responsible for modelling the data. • The Resource Model was effectively a Data Mapper. • Any business logic was either in the Model or in a custom built service class @matthewhaworth
  • 6. Magento 2’s New Architecture • Magento 2 seeks to formalise this structure by introducing ‘Service Contracts’. • Service contracts are a set of interfaces defined for data models and services. @matthewhaworth
  • 8. Magento 2’s New Architecture • Data persistence within service contracts starts with the ’repository’. • “A Repository mediates between the domain and data mapping layers, acting like an in-memory domain object collection.” – Martin Fowler @matthewhaworth
  • 9. No more abstract model! • Domain objects are now completely independent from the database! @matthewhaworth Model Abstract Model extends
  • 10. @matthewhaworth Magento 1 Magento 2 Model Abstract Model Resource Model Abstract Resource Model extends extends delegates database map and save Repository Interface Repository Implementation implements Data Interface Data Object Implementation implements is passed to repository for save
  • 11. @matthewhaworth Magento 1 Magento 2 Model Abstract Model Resource Model Abstract Resource Model extends extends delegates database map and save Resource Model Abstract Resource Model extends Repository Interface Repository Implementation implements Data Interface Data Object Implementation implements delegates database map and save is passed to repository for save
  • 12. Why move to repositories? • Separate the representation of data, business logic and data persistence • Beneficial for testing by allowing a substitution point for unit tests • Repository interfaces provides a version-able, single point of communication for APIs and other modular dependencies @matthewhaworth
  • 13. But wait, there’s a problem… The object passed to the resource must extend the Abstract Model public function save(MagentoFrameworkModelAbstractModel $object) @matthewhaworth Resource ModelRepository InterfaceData Interface
  • 14. So what are our options? 1. Implement data interface and extend the Abstract Model, sacrificing our new found independence. @matthewhaworth Data Object Abstract ModelData Interface implements extends
  • 15. So what are our options? 2. Implement the data interface and map to the Abstract Model in the repository before passing to the resource. @matthewhaworth Abstract Model Implementation Data Object Repository Resource map
  • 16. So what are our options? 3. Best of both worlds! Implement the data interface, the abstract model and map to the Abstract Model in the repository before passing to the resource. @matthewhaworth Model & Data Object Repository Resource 2. Map to data object Abstract ModelData Interface implements extends 1. Submit to save Model & Data Object
  • 17. So what are our options? 4. Use a different data persistence layer to the Magento DB Resource, maybe Doctrine? @matthewhaworth
  • 18. So what are our options? 4. Use a different data persistence layer to the Magento DB Resource, maybe Doctrine? @matthewhaworth Nah.
  • 19. For me, the clear winner is approach 3 • Implement the data interface and extend the abstract model • Map from the data interface to the abstract model in the repository • Avoids duplicating an extra model for the sake of pushing to the resource @matthewhaworth Model & Data Object Repository Resource 2. Map to data object Abstract Model Data Interface implementsextends 1. Submit to save Model & Data Object
  • 21. Where can I see good examples? Magento_Customer was regarded as the best example. This module maps its data interfaces to a separate Abstract Model implementation in its repository (approach 2). Multi Source Inventory (a set of modules) is currently regarded as the best example to follow. Data models in this module both implement the data interface and extend the data model (approach 3) and also make use of CQRS. (see MagentoInventoryModelStock @ github.com/magento-engcom/msi) @matthewhaworth
  • 22. What does the future look like? • Removing the Abstract Model entirely • Moving beforeSave(), afterSave(), etc to plugins and events (AOP) (They’re also making them ‘bulk-first’.) • Command Query Responsibility Segregation @matthewhaworth
  • 23. Thank you to Alan Kent, Anton Kril and Vinai Kopp @matthewhaworth Questions?