Magento 2 Technical Direction · Command & Query Responsibility Segregation (CQRS) Incorrect Server...
Transcript of Magento 2 Technical Direction · Command & Query Responsibility Segregation (CQRS) Incorrect Server...
![Page 1: Magento 2 Technical Direction · Command & Query Responsibility Segregation (CQRS) Incorrect Server MVC • Separation of Presentation from Model is preserved • Violates HTTP (CQRS)](https://reader033.fdocuments.us/reader033/viewer/2022042515/5f635a8fc7f3842a882dc642/html5/thumbnails/1.jpg)
![Page 2: Magento 2 Technical Direction · Command & Query Responsibility Segregation (CQRS) Incorrect Server MVC • Separation of Presentation from Model is preserved • Violates HTTP (CQRS)](https://reader033.fdocuments.us/reader033/viewer/2022042515/5f635a8fc7f3842a882dc642/html5/thumbnails/2.jpg)
Magento 2 Technical
Direction
![Page 3: Magento 2 Technical Direction · Command & Query Responsibility Segregation (CQRS) Incorrect Server MVC • Separation of Presentation from Model is preserved • Violates HTTP (CQRS)](https://reader033.fdocuments.us/reader033/viewer/2022042515/5f635a8fc7f3842a882dc642/html5/thumbnails/3.jpg)
Chief Architect, Magento Commerce
Alan Kent
![Page 4: Magento 2 Technical Direction · Command & Query Responsibility Segregation (CQRS) Incorrect Server MVC • Separation of Presentation from Model is preserved • Violates HTTP (CQRS)](https://reader033.fdocuments.us/reader033/viewer/2022042515/5f635a8fc7f3842a882dc642/html5/thumbnails/4.jpg)
Goals of Continuous Platform Improvement
• Improve platform performance
• Reduce complexity
• Increase code consistency
• Reduce extension conflicts
![Page 5: Magento 2 Technical Direction · Command & Query Responsibility Segregation (CQRS) Incorrect Server MVC • Separation of Presentation from Model is preserved • Violates HTTP (CQRS)](https://reader033.fdocuments.us/reader033/viewer/2022042515/5f635a8fc7f3842a882dc642/html5/thumbnails/5.jpg)
Agenda
• Magento MVC & CQRS
• Persistence
• Customer Data & Sessions
• Inheritance Based APIs
• Classes vs Namespaces
• Deployment
![Page 6: Magento 2 Technical Direction · Command & Query Responsibility Segregation (CQRS) Incorrect Server MVC • Separation of Presentation from Model is preserved • Violates HTTP (CQRS)](https://reader033.fdocuments.us/reader033/viewer/2022042515/5f635a8fc7f3842a882dc642/html5/thumbnails/6.jpg)
MVC & CQRS
![Page 7: Magento 2 Technical Direction · Command & Query Responsibility Segregation (CQRS) Incorrect Server MVC • Separation of Presentation from Model is preserved • Violates HTTP (CQRS)](https://reader033.fdocuments.us/reader033/viewer/2022042515/5f635a8fc7f3842a882dc642/html5/thumbnails/7.jpg)
MVC
• Model - This is the part of your application
that defines its basic functionality behind a
set of abstractions. Data access routines
and some business logic can be defined in
the model.
• View - Views define exactly what is
presented to the user. Usually controllers
pass data to each view to render in some
format…
• Controller - Controllers bind the whole
pattern together. They manipulate models,
decide which view to display...
https://framework.zend.com
![Page 8: Magento 2 Technical Direction · Command & Query Responsibility Segregation (CQRS) Incorrect Server MVC • Separation of Presentation from Model is preserved • Violates HTTP (CQRS)](https://reader033.fdocuments.us/reader033/viewer/2022042515/5f635a8fc7f3842a882dc642/html5/thumbnails/8.jpg)
MVC comes from client applications
![Page 9: Magento 2 Technical Direction · Command & Query Responsibility Segregation (CQRS) Incorrect Server MVC • Separation of Presentation from Model is preserved • Violates HTTP (CQRS)](https://reader033.fdocuments.us/reader033/viewer/2022042515/5f635a8fc7f3842a882dc642/html5/thumbnails/9.jpg)
Fat client MVC
• Presentation is separate from Model
• State modification is separate from retrieval
![Page 10: Magento 2 Technical Direction · Command & Query Responsibility Segregation (CQRS) Incorrect Server MVC • Separation of Presentation from Model is preserved • Violates HTTP (CQRS)](https://reader033.fdocuments.us/reader033/viewer/2022042515/5f635a8fc7f3842a882dc642/html5/thumbnails/10.jpg)
Command & Query Responsibility
Segregation (CQRS)
![Page 11: Magento 2 Technical Direction · Command & Query Responsibility Segregation (CQRS) Incorrect Server MVC • Separation of Presentation from Model is preserved • Violates HTTP (CQRS)](https://reader033.fdocuments.us/reader033/viewer/2022042515/5f635a8fc7f3842a882dc642/html5/thumbnails/11.jpg)
Incorrect Server MVC
• Separation of Presentation from Model is preserved
• Violates HTTP (CQRS)
• State modification is done by same infrastructure as state retrieval (violates
CQRS)
![Page 12: Magento 2 Technical Direction · Command & Query Responsibility Segregation (CQRS) Incorrect Server MVC • Separation of Presentation from Model is preserved • Violates HTTP (CQRS)](https://reader033.fdocuments.us/reader033/viewer/2022042515/5f635a8fc7f3842a882dc642/html5/thumbnails/12.jpg)
Less Incorrect Server MVC
• Separation of Presentation from Model is preserved
• Obeys HTTP
• State modification is done by same infrastructure as state retrieval
![Page 13: Magento 2 Technical Direction · Command & Query Responsibility Segregation (CQRS) Incorrect Server MVC • Separation of Presentation from Model is preserved • Violates HTTP (CQRS)](https://reader033.fdocuments.us/reader033/viewer/2022042515/5f635a8fc7f3842a882dc642/html5/thumbnails/13.jpg)
Correct Server MVC
• Separation of Presentation from Model is preserved
• Obeys HTTP
• State modification and retrieval is partly done by different infrastructures
![Page 14: Magento 2 Technical Direction · Command & Query Responsibility Segregation (CQRS) Incorrect Server MVC • Separation of Presentation from Model is preserved • Violates HTTP (CQRS)](https://reader033.fdocuments.us/reader033/viewer/2022042515/5f635a8fc7f3842a882dc642/html5/thumbnails/14.jpg)
Correct MVC with CQRS
• Separation of Presentation from Model is preserved
• Obeys HTTP
• State modification and retrieval is done by different infrastructures
![Page 15: Magento 2 Technical Direction · Command & Query Responsibility Segregation (CQRS) Incorrect Server MVC • Separation of Presentation from Model is preserved • Violates HTTP (CQRS)](https://reader033.fdocuments.us/reader033/viewer/2022042515/5f635a8fc7f3842a882dc642/html5/thumbnails/15.jpg)
CQRS
• Separate Query API & Command API
• Command API – optimized for write, have business logic
• Query API – optimized for read, cacheable
• Query actualization through indexing
![Page 16: Magento 2 Technical Direction · Command & Query Responsibility Segregation (CQRS) Incorrect Server MVC • Separation of Presentation from Model is preserved • Violates HTTP (CQRS)](https://reader033.fdocuments.us/reader033/viewer/2022042515/5f635a8fc7f3842a882dc642/html5/thumbnails/16.jpg)
Data Persistence
evolution
![Page 17: Magento 2 Technical Direction · Command & Query Responsibility Segregation (CQRS) Incorrect Server MVC • Separation of Presentation from Model is preserved • Violates HTTP (CQRS)](https://reader033.fdocuments.us/reader033/viewer/2022042515/5f635a8fc7f3842a882dc642/html5/thumbnails/17.jpg)
Persistence
class AbstractModel
{
/**
* @deprecated
*/
public function save()
// …
/**
* @deprecated
*/
public function delete()
// …
}
![Page 18: Magento 2 Technical Direction · Command & Query Responsibility Segregation (CQRS) Incorrect Server MVC • Separation of Presentation from Model is preserved • Violates HTTP (CQRS)](https://reader033.fdocuments.us/reader033/viewer/2022042515/5f635a8fc7f3842a882dc642/html5/thumbnails/18.jpg)
Persistence of entities should be separate from
entities (element of DDD)
![Page 19: Magento 2 Technical Direction · Command & Query Responsibility Segregation (CQRS) Incorrect Server MVC • Separation of Presentation from Model is preserved • Violates HTTP (CQRS)](https://reader033.fdocuments.us/reader033/viewer/2022042515/5f635a8fc7f3842a882dc642/html5/thumbnails/19.jpg)
Persistence
$this->productResourceModel->save($product);
![Page 20: Magento 2 Technical Direction · Command & Query Responsibility Segregation (CQRS) Incorrect Server MVC • Separation of Presentation from Model is preserved • Violates HTTP (CQRS)](https://reader033.fdocuments.us/reader033/viewer/2022042515/5f635a8fc7f3842a882dc642/html5/thumbnails/20.jpg)
Services Interface
class ProductRepository implements ProductRepositoryInterface
{
public function createProduct(ProductInterface $product)
{
// All the logic goes here
}
}
![Page 21: Magento 2 Technical Direction · Command & Query Responsibility Segregation (CQRS) Incorrect Server MVC • Separation of Presentation from Model is preserved • Violates HTTP (CQRS)](https://reader033.fdocuments.us/reader033/viewer/2022042515/5f635a8fc7f3842a882dc642/html5/thumbnails/21.jpg)
Data Interface
interface ProductInterface
{
public function getSku();
public function setSku();
public function getName();
public function setName();
// only getters & setters
}
![Page 22: Magento 2 Technical Direction · Command & Query Responsibility Segregation (CQRS) Incorrect Server MVC • Separation of Presentation from Model is preserved • Violates HTTP (CQRS)](https://reader033.fdocuments.us/reader033/viewer/2022042515/5f635a8fc7f3842a882dc642/html5/thumbnails/22.jpg)
Service Contracts expose procedural API
![Page 23: Magento 2 Technical Direction · Command & Query Responsibility Segregation (CQRS) Incorrect Server MVC • Separation of Presentation from Model is preserved • Violates HTTP (CQRS)](https://reader033.fdocuments.us/reader033/viewer/2022042515/5f635a8fc7f3842a882dc642/html5/thumbnails/23.jpg)
Persistence
• OOP does not work well with Microservices
• No logic on models
• Persistence – just mapping data transfer objects to DB
![Page 24: Magento 2 Technical Direction · Command & Query Responsibility Segregation (CQRS) Incorrect Server MVC • Separation of Presentation from Model is preserved • Violates HTTP (CQRS)](https://reader033.fdocuments.us/reader033/viewer/2022042515/5f635a8fc7f3842a882dc642/html5/thumbnails/24.jpg)
Customer DATA &
Sessions
![Page 25: Magento 2 Technical Direction · Command & Query Responsibility Segregation (CQRS) Incorrect Server MVC • Separation of Presentation from Model is preserved • Violates HTTP (CQRS)](https://reader033.fdocuments.us/reader033/viewer/2022042515/5f635a8fc7f3842a882dc642/html5/thumbnails/25.jpg)
Request flow
Customer-agnostic content
(all routes)
Customer Data API (/sections/…)
![Page 26: Magento 2 Technical Direction · Command & Query Responsibility Segregation (CQRS) Incorrect Server MVC • Separation of Presentation from Model is preserved • Violates HTTP (CQRS)](https://reader033.fdocuments.us/reader033/viewer/2022042515/5f635a8fc7f3842a882dc642/html5/thumbnails/26.jpg)
Customer Data & Sessions
• No session usage in blocks
• Only Sections Controller knows about session
![Page 27: Magento 2 Technical Direction · Command & Query Responsibility Segregation (CQRS) Incorrect Server MVC • Separation of Presentation from Model is preserved • Violates HTTP (CQRS)](https://reader033.fdocuments.us/reader033/viewer/2022042515/5f635a8fc7f3842a882dc642/html5/thumbnails/27.jpg)
Discouraging
Inheritance
![Page 28: Magento 2 Technical Direction · Command & Query Responsibility Segregation (CQRS) Incorrect Server MVC • Separation of Presentation from Model is preserved • Violates HTTP (CQRS)](https://reader033.fdocuments.us/reader033/viewer/2022042515/5f635a8fc7f3842a882dc642/html5/thumbnails/28.jpg)
Class
Method
Code Reuse Options
Behavior
Parent Trait
DependencyFacade
Private
![Page 29: Magento 2 Technical Direction · Command & Query Responsibility Segregation (CQRS) Incorrect Server MVC • Separation of Presentation from Model is preserved • Violates HTTP (CQRS)](https://reader033.fdocuments.us/reader033/viewer/2022042515/5f635a8fc7f3842a882dc642/html5/thumbnails/29.jpg)
Problems With Inheritance
• Misused in 99% of cases
• Child depends on parent __construct()
• Static coupling – can’t substitute/decorate parent
• Performance problem: all parent dependencies are inherited
• Look at Product Model
![Page 30: Magento 2 Technical Direction · Command & Query Responsibility Segregation (CQRS) Incorrect Server MVC • Separation of Presentation from Model is preserved • Violates HTTP (CQRS)](https://reader033.fdocuments.us/reader033/viewer/2022042515/5f635a8fc7f3842a882dc642/html5/thumbnails/30.jpg)
Bad for performance! Kill!
![Page 31: Magento 2 Technical Direction · Command & Query Responsibility Segregation (CQRS) Incorrect Server MVC • Separation of Presentation from Model is preserved • Violates HTTP (CQRS)](https://reader033.fdocuments.us/reader033/viewer/2022042515/5f635a8fc7f3842a882dc642/html5/thumbnails/31.jpg)
Inheritance-based APIs
• Model -> DTOs
• Resource Model -> Data Mapping
• Collection -> Query API
• Block -> Interface subtyping
• Controller -> Interface subtyping
• …
![Page 32: Magento 2 Technical Direction · Command & Query Responsibility Segregation (CQRS) Incorrect Server MVC • Separation of Presentation from Model is preserved • Violates HTTP (CQRS)](https://reader033.fdocuments.us/reader033/viewer/2022042515/5f635a8fc7f3842a882dc642/html5/thumbnails/32.jpg)
Classes VS
Namespaces
![Page 33: Magento 2 Technical Direction · Command & Query Responsibility Segregation (CQRS) Incorrect Server MVC • Separation of Presentation from Model is preserved • Violates HTTP (CQRS)](https://reader033.fdocuments.us/reader033/viewer/2022042515/5f635a8fc7f3842a882dc642/html5/thumbnails/33.jpg)
Classes VS Namespaces
class IndexController
{
public function firstAction()
{
//…
}
public function secondAction()
{
//…
}
}
![Page 34: Magento 2 Technical Direction · Command & Query Responsibility Segregation (CQRS) Incorrect Server MVC • Separation of Presentation from Model is preserved • Violates HTTP (CQRS)](https://reader033.fdocuments.us/reader033/viewer/2022042515/5f635a8fc7f3842a882dc642/html5/thumbnails/34.jpg)
Classes VS Namespaces
class IndexController
{
public function __construct($dependencyA, $dependencyB)
//…
public function firstAction()
{
$this->dependencyA->doSmth();
}
public function secondAction()
{
$this->dependencyB->doSmth();
}
}
![Page 35: Magento 2 Technical Direction · Command & Query Responsibility Segregation (CQRS) Incorrect Server MVC • Separation of Presentation from Model is preserved • Violates HTTP (CQRS)](https://reader033.fdocuments.us/reader033/viewer/2022042515/5f635a8fc7f3842a882dc642/html5/thumbnails/35.jpg)
Classes VS Namespaces
class First implements Magento\Framework\App\ActionInterface
{
public function __construct($dependencyA)
//…
public function execute()
{
$this->dependencyA->doSmth();
}
}
![Page 36: Magento 2 Technical Direction · Command & Query Responsibility Segregation (CQRS) Incorrect Server MVC • Separation of Presentation from Model is preserved • Violates HTTP (CQRS)](https://reader033.fdocuments.us/reader033/viewer/2022042515/5f635a8fc7f3842a882dc642/html5/thumbnails/36.jpg)
Deployment
improvements
![Page 37: Magento 2 Technical Direction · Command & Query Responsibility Segregation (CQRS) Incorrect Server MVC • Separation of Presentation from Model is preserved • Violates HTTP (CQRS)](https://reader033.fdocuments.us/reader033/viewer/2022042515/5f635a8fc7f3842a882dc642/html5/thumbnails/37.jpg)
Deployment theory
Development Staging Production
![Page 38: Magento 2 Technical Direction · Command & Query Responsibility Segregation (CQRS) Incorrect Server MVC • Separation of Presentation from Model is preserved • Violates HTTP (CQRS)](https://reader033.fdocuments.us/reader033/viewer/2022042515/5f635a8fc7f3842a882dc642/html5/thumbnails/38.jpg)
How people do
Development Production
• Install DB
• Compile
• Deploy Static
![Page 39: Magento 2 Technical Direction · Command & Query Responsibility Segregation (CQRS) Incorrect Server MVC • Separation of Presentation from Model is preserved • Violates HTTP (CQRS)](https://reader033.fdocuments.us/reader033/viewer/2022042515/5f635a8fc7f3842a882dc642/html5/thumbnails/39.jpg)
How it’s supposed to work
Development Staging Production
• Compile
• Deploy Static
• Install DB • Install DB
![Page 40: Magento 2 Technical Direction · Command & Query Responsibility Segregation (CQRS) Incorrect Server MVC • Separation of Presentation from Model is preserved • Violates HTTP (CQRS)](https://reader033.fdocuments.us/reader033/viewer/2022042515/5f635a8fc7f3842a882dc642/html5/thumbnails/40.jpg)
Magento Deployment Improvements
• Compilation & static deployment DB-independent
• Allow to store System Configuration to files
• Move part of initial data to files (Stores, Theme config)
![Page 41: Magento 2 Technical Direction · Command & Query Responsibility Segregation (CQRS) Incorrect Server MVC • Separation of Presentation from Model is preserved • Violates HTTP (CQRS)](https://reader033.fdocuments.us/reader033/viewer/2022042515/5f635a8fc7f3842a882dc642/html5/thumbnails/41.jpg)
In summary we will:
• Continue applying CQRS on framework level
• Continue micro-service-ready approach
• Continue moving session management to browser
• Discourage inheritance and abandon inheritance-based APIs
• Stop using classes as namespaces
• Separate configuration from code
![Page 42: Magento 2 Technical Direction · Command & Query Responsibility Segregation (CQRS) Incorrect Server MVC • Separation of Presentation from Model is preserved • Violates HTTP (CQRS)](https://reader033.fdocuments.us/reader033/viewer/2022042515/5f635a8fc7f3842a882dc642/html5/thumbnails/42.jpg)
Q & A