SlideShare una empresa de Scribd logo
1 de 23
Descargar para leer sin conexión
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?

Más contenido relacionado

Similar 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 FrameworkAkhil Mittal
 
Design Patterns
Design PatternsDesign Patterns
Design Patternsimedo.de
 
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 2Mathew Beane
 
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 weili_at_slideshare
 
What is struts_en
What is struts_enWhat is struts_en
What is struts_entechbed
 
Struts 2-overview2
Struts 2-overview2Struts 2-overview2
Struts 2-overview2divzi1913
 
common design patterns summary.pdf
common design patterns summary.pdfcommon design patterns summary.pdf
common design patterns summary.pdfNikolayRaychev2
 
Server side programming bt0083
Server side programming bt0083Server side programming bt0083
Server side programming bt0083Divyam Pateriya
 
Architecting an ASP.NET MVC Solution
Architecting an ASP.NET MVC SolutionArchitecting an ASP.NET MVC Solution
Architecting an ASP.NET MVC SolutionAndrea Saltarello
 
Spring data presentation
Spring data presentationSpring data presentation
Spring data presentationOleksii Usyk
 
The Meteor Framework
The Meteor FrameworkThe Meteor Framework
The Meteor FrameworkDamien Magoni
 
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)Clarence Ngoh
 
MidwestPHP - Getting Started with Magento 2
MidwestPHP - Getting Started with Magento 2MidwestPHP - Getting Started with Magento 2
MidwestPHP - Getting Started with Magento 2Mathew Beane
 
Reactive data analysis with vert.x
Reactive data analysis with vert.xReactive data analysis with vert.x
Reactive data analysis with vert.xGerald Muecke
 

Similar 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
 
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
 
J2EE pattern 5
J2EE pattern 5J2EE pattern 5
J2EE pattern 5
 

Último

Comparing Linux OS Image Update Models - EOSS 2024.pdf
Comparing Linux OS Image Update Models - EOSS 2024.pdfComparing Linux OS Image Update Models - EOSS 2024.pdf
Comparing Linux OS Image Update Models - EOSS 2024.pdfDrew Moseley
 
A healthy diet for your Java application Devoxx France.pdf
A healthy diet for your Java application Devoxx France.pdfA healthy diet for your Java application Devoxx France.pdf
A healthy diet for your Java application Devoxx France.pdfMarharyta Nedzelska
 
What is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need ItWhat is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need ItWave PLM
 
Software Coding for software engineering
Software Coding for software engineeringSoftware Coding for software engineering
Software Coding for software engineeringssuserb3a23b
 
Folding Cheat Sheet #4 - fourth in a series
Folding Cheat Sheet #4 - fourth in a seriesFolding Cheat Sheet #4 - fourth in a series
Folding Cheat Sheet #4 - fourth in a seriesPhilip Schwarz
 
Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024Andreas Granig
 
Recruitment Management Software Benefits (Infographic)
Recruitment Management Software Benefits (Infographic)Recruitment Management Software Benefits (Infographic)
Recruitment Management Software Benefits (Infographic)Hr365.us smith
 
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptxKnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptxTier1 app
 
Introduction Computer Science - Software Design.pdf
Introduction Computer Science - Software Design.pdfIntroduction Computer Science - Software Design.pdf
Introduction Computer Science - Software Design.pdfFerryKemperman
 
What is Advanced Excel and what are some best practices for designing and cre...
What is Advanced Excel and what are some best practices for designing and cre...What is Advanced Excel and what are some best practices for designing and cre...
What is Advanced Excel and what are some best practices for designing and cre...Technogeeks
 
Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...
Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...
Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...Natan Silnitsky
 
Ahmed Motair CV April 2024 (Senior SW Developer)
Ahmed Motair CV April 2024 (Senior SW Developer)Ahmed Motair CV April 2024 (Senior SW Developer)
Ahmed Motair CV April 2024 (Senior SW Developer)Ahmed Mater
 
SpotFlow: Tracking Method Calls and States at Runtime
SpotFlow: Tracking Method Calls and States at RuntimeSpotFlow: Tracking Method Calls and States at Runtime
SpotFlow: Tracking Method Calls and States at Runtimeandrehoraa
 
Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...
Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...
Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...OnePlan Solutions
 
Unveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML DiagramsUnveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML DiagramsAhmed Mohamed
 
Unveiling the Future: Sylius 2.0 New Features
Unveiling the Future: Sylius 2.0 New FeaturesUnveiling the Future: Sylius 2.0 New Features
Unveiling the Future: Sylius 2.0 New FeaturesŁukasz Chruściel
 
Intelligent Home Wi-Fi Solutions | ThinkPalm
Intelligent Home Wi-Fi Solutions | ThinkPalmIntelligent Home Wi-Fi Solutions | ThinkPalm
Intelligent Home Wi-Fi Solutions | ThinkPalmSujith Sukumaran
 
React Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief UtamaReact Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief UtamaHanief Utama
 

Último (20)

Comparing Linux OS Image Update Models - EOSS 2024.pdf
Comparing Linux OS Image Update Models - EOSS 2024.pdfComparing Linux OS Image Update Models - EOSS 2024.pdf
Comparing Linux OS Image Update Models - EOSS 2024.pdf
 
A healthy diet for your Java application Devoxx France.pdf
A healthy diet for your Java application Devoxx France.pdfA healthy diet for your Java application Devoxx France.pdf
A healthy diet for your Java application Devoxx France.pdf
 
Hot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort Service
Hot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort ServiceHot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort Service
Hot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort Service
 
What is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need ItWhat is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need It
 
Software Coding for software engineering
Software Coding for software engineeringSoftware Coding for software engineering
Software Coding for software engineering
 
Folding Cheat Sheet #4 - fourth in a series
Folding Cheat Sheet #4 - fourth in a seriesFolding Cheat Sheet #4 - fourth in a series
Folding Cheat Sheet #4 - fourth in a series
 
Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024
 
Recruitment Management Software Benefits (Infographic)
Recruitment Management Software Benefits (Infographic)Recruitment Management Software Benefits (Infographic)
Recruitment Management Software Benefits (Infographic)
 
Advantages of Odoo ERP 17 for Your Business
Advantages of Odoo ERP 17 for Your BusinessAdvantages of Odoo ERP 17 for Your Business
Advantages of Odoo ERP 17 for Your Business
 
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptxKnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
 
Introduction Computer Science - Software Design.pdf
Introduction Computer Science - Software Design.pdfIntroduction Computer Science - Software Design.pdf
Introduction Computer Science - Software Design.pdf
 
What is Advanced Excel and what are some best practices for designing and cre...
What is Advanced Excel and what are some best practices for designing and cre...What is Advanced Excel and what are some best practices for designing and cre...
What is Advanced Excel and what are some best practices for designing and cre...
 
Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...
Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...
Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...
 
Ahmed Motair CV April 2024 (Senior SW Developer)
Ahmed Motair CV April 2024 (Senior SW Developer)Ahmed Motair CV April 2024 (Senior SW Developer)
Ahmed Motair CV April 2024 (Senior SW Developer)
 
SpotFlow: Tracking Method Calls and States at Runtime
SpotFlow: Tracking Method Calls and States at RuntimeSpotFlow: Tracking Method Calls and States at Runtime
SpotFlow: Tracking Method Calls and States at Runtime
 
Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...
Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...
Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...
 
Unveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML DiagramsUnveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML Diagrams
 
Unveiling the Future: Sylius 2.0 New Features
Unveiling the Future: Sylius 2.0 New FeaturesUnveiling the Future: Sylius 2.0 New Features
Unveiling the Future: Sylius 2.0 New Features
 
Intelligent Home Wi-Fi Solutions | ThinkPalm
Intelligent Home Wi-Fi Solutions | ThinkPalmIntelligent Home Wi-Fi Solutions | ThinkPalm
Intelligent Home Wi-Fi Solutions | ThinkPalm
 
React Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief UtamaReact Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief Utama
 

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?