magento 2 technical direction · command & query responsibility segregation (cqrs) incorrect...

42

Upload: others

Post on 22-Jul-2020

14 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Magento 2 Technical Direction · Command & Query Responsibility Segregation (CQRS) Incorrect Server MVC • Separation of Presentation from Model is preserved • Violates HTTP (CQRS)
Page 2: Magento 2 Technical Direction · Command & Query Responsibility Segregation (CQRS) Incorrect Server MVC • Separation of Presentation from Model is preserved • Violates HTTP (CQRS)

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)

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)

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)

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)

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)

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)

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)

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)

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)

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)

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)

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)

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)

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)

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)

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)

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)

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)

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)

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)

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)

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)

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)

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)

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)

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)

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)

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)

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)

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)

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)

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)

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)

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)

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)

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)

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)

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)

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)

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)

Q & A