magento 2 technical direction · command & query responsibility segregation (cqrs) incorrect...
TRANSCRIPT
Magento 2 Technical
Direction
Chief Architect, Magento Commerce
Alan Kent
Goals of Continuous Platform Improvement
• Improve platform performance
• Reduce complexity
• Increase code consistency
• Reduce extension conflicts
Agenda
• Magento MVC & CQRS
• Persistence
• Customer Data & Sessions
• Inheritance Based APIs
• Classes vs Namespaces
• Deployment
MVC & CQRS
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
MVC comes from client applications
Fat client MVC
• Presentation is separate from Model
• State modification is separate from retrieval
Command & Query Responsibility
Segregation (CQRS)
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)
Less Incorrect Server MVC
• Separation of Presentation from Model is preserved
• Obeys HTTP
• State modification is done by same infrastructure as state retrieval
Correct Server MVC
• Separation of Presentation from Model is preserved
• Obeys HTTP
• State modification and retrieval is partly done by different infrastructures
Correct MVC with CQRS
• Separation of Presentation from Model is preserved
• Obeys HTTP
• State modification and retrieval is done by different infrastructures
CQRS
• Separate Query API & Command API
• Command API – optimized for write, have business logic
• Query API – optimized for read, cacheable
• Query actualization through indexing
Data Persistence
evolution
Persistence
class AbstractModel
{
/**
* @deprecated
*/
public function save()
// …
/**
* @deprecated
*/
public function delete()
// …
}
Persistence of entities should be separate from
entities (element of DDD)
Persistence
$this->productResourceModel->save($product);
Services Interface
class ProductRepository implements ProductRepositoryInterface
{
public function createProduct(ProductInterface $product)
{
// All the logic goes here
}
}
Data Interface
interface ProductInterface
{
public function getSku();
public function setSku();
public function getName();
public function setName();
// only getters & setters
}
Service Contracts expose procedural API
Persistence
• OOP does not work well with Microservices
• No logic on models
• Persistence – just mapping data transfer objects to DB
Customer DATA &
Sessions
Request flow
Customer-agnostic content
(all routes)
Customer Data API (/sections/…)
Customer Data & Sessions
• No session usage in blocks
• Only Sections Controller knows about session
Discouraging
Inheritance
Class
Method
Code Reuse Options
Behavior
Parent Trait
DependencyFacade
Private
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
Bad for performance! Kill!
Inheritance-based APIs
• Model -> DTOs
• Resource Model -> Data Mapping
• Collection -> Query API
• Block -> Interface subtyping
• Controller -> Interface subtyping
• …
Classes VS
Namespaces
Classes VS Namespaces
class IndexController
{
public function firstAction()
{
//…
}
public function secondAction()
{
//…
}
}
Classes VS Namespaces
class IndexController
{
public function __construct($dependencyA, $dependencyB)
//…
public function firstAction()
{
$this->dependencyA->doSmth();
}
public function secondAction()
{
$this->dependencyB->doSmth();
}
}
Classes VS Namespaces
class First implements Magento\Framework\App\ActionInterface
{
public function __construct($dependencyA)
//…
public function execute()
{
$this->dependencyA->doSmth();
}
}
Deployment
improvements
Deployment theory
Development Staging Production
How people do
Development Production
• Install DB
• Compile
• Deploy Static
How it’s supposed to work
Development Staging Production
• Compile
• Deploy Static
• Install DB • Install DB
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)
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
Q & A