September 18, 2014 Modular development in Magento 2 Igor Miniailo Magento
Agenda 1 Magento 2 goals 2 Magento 1 modules 3 Decoupling techniques 4 Magento 2 is it getting better? 5 Modularity examples
Magento 2 Goals Modern tech stack Improved performance and scalability Streamline customizations Simplify external integrations Easy installation and upgrades High code quality and tests
Modules. Definition Modules serve as named containers for domain object classes that are highly cohesive with one another. The goal should be low coupling between the classes that are in different Modules
2 Magento 1 Modules.
Magento 1 Modules While Magento has always had a modular architecture, the current module boundaries are not always ideal.
3 Decoupling techniques.
Relationship between an interface and its implementation. An implementation depends on its interface but not vice versa. In particular, any caller of an interface depends only on the interface, even if a separate module implements it.
Defining an interface in a module that a separate module intends to implement is a fundamental way to break dependencies and reduce coupling.
Some more techniques Dependency injection Interceptors (ability to observe public/protected method calls in application) Tell, don t ask Law Of Demeter CQS and CQRS
Dependency injection Dependency Injection achieves Decoupling using Inversion of Control. * Constructor injection It s the technique of passing objects dependencies to its constructor. ** Note that the constructor accepts an interface and not concrete object.
Interceptors
Tell, don t ask Procedural code gets information then makes decisions. Object-oriented code tells objects to do things. Alec Sharp * To make decisions outside the object violates its encapsulation ** The fundamental principle of Object Oriented programming is the unification of methods and data. Splitting this up inappropriately gets you right back to procedural programming
Law of Demeter Only talk to your immediate friends. You can play with yourself. You can play with your own toys (but you can't take them apart), You can play with toys that were given to you. And you can play with toys you've made yourself. * Never call a method on an object you got from another call nor on a global object.
CQS / CQRS - command query separation To ask is a query, to tell is a command * Getters must not change state of the object Command Query Responsibility Segregation (CQRS) applies the CQS principle by using separate Query and Command objects to retrieve and modify data respectively.
Simple rules for module design: Ensuring that Modules are largely independent of others has the same benefit as loosely coupled classes. This will make it easier to maintain and refactor code inside the module. Do strive for acyclic dependencies on Modules where coupling is necessary. Just unidirection
4 Magento 2 Modules. Is it getting better?
Framework vs Modules Isolate the expression of the domain model and the business logic from underlying infrastructure layer. Partition a complex program into layers. Develop a design within each layer that is cohesive and that depends only on the layers below. Conclusion: The core framework functionality should be in the framework, not mixed in with modules.
Framework. Usage of 3 rd party components in Core - Zend Modules layer Framework Layer External component Layer
Framework magento2/lib/internal/magento/framework/*
Module.xml
5 Modularity examples.
Modularity Examples. Configuration Problem: Store Config Model Coupling on Store (Core module)
Modularity Configuration. Solution
Modularity Example. Shipping carriers Problem: All shipping carriers are delivered in one module
Modularity Shipping carriers. Solution
Magento 2 Decoupling
Documentation https://wiki.magento.com/display/mage2doc/magento+1.x+to+2.x+backwards-incompatible +Changes https://wiki.magento.com/display/mage2doc/modularity%3a+core+components+moved+from +the+application+to+framework+layer https://wiki.magento.com/display/mage2doc/modularity%3a+core+components+moved+to +Store+Module
Last notice Don t forget Unit test your code, because the essence of Unit test to reveal coupling