Jisse Reitsma - Messing up dependency injection
Transcript of Jisse Reitsma - Messing up dependency injection
Messing UpMessing Up
Dependency Injection
Messing Up DI
www.yireo.com - slides.yireo.com
About meAbout me
Jisse Reitsma
Messing Up DI
www.yireo.com - slides.yireo.com
Messing UpMessing Up
Dependency Injection
Messing Up DI
www.yireo.com - slides.yireo.com
About meAbout me
Jisse Reitsma
Founder and lead developer of Yireo
Messing Up DI
www.yireo.com - slides.yireo.com
About meAbout me
Jisse Reitsma
Messing Up DI
www.yireo.com - slides.yireo.com
About meAbout me
Jisse Reitsma
Founder and lead developer of Yireo
Trainer, enterpreneur, coder
Messing Up DI
www.yireo.com - slides.yireo.com
About meAbout me
Jisse Reitsma
Founder and lead developer of Yireo
Messing Up DI
www.yireo.com - slides.yireo.com
About meAbout me
Jisse Reitsma
Founder and lead developer of Yireo
Trainer, enterpreneur, coder
Wrote 2 developer books for J**ml*
Messing Up DI
www.yireo.com - slides.yireo.com
About meAbout me
Jisse Reitsma
Founder and lead developer of Yireo
Trainer, enterpreneur, coder
Messing Up DI
www.yireo.com - slides.yireo.com
About meAbout me
Jisse Reitsma
Founder and lead developer of Yireo
Trainer, enterpreneur, coder
Wrote 2 developer books for J**ml*
Loving Magento 2
Messing Up DI
www.yireo.com - slides.yireo.com
About meAbout me
Jisse Reitsma
Founder and lead developer of Yireo
Trainer, enterpreneur, coder
Wrote 2 developer books for J**ml*
Messing Up DI
www.yireo.com - slides.yireo.com
About meAbout me
Jisse Reitsma
Founder and lead developer of Yireo
Trainer, enterpreneur, coder
Wrote 2 developer books for J**ml*
Loving Magento 2
I live near a palace
Messing Up DI
www.yireo.com - slides.yireo.com
About meAbout me
Jisse Reitsma
Founder and lead developer of Yireo
Trainer, enterpreneur, coder
Wrote 2 developer books for J**ml*
Loving Magento 2
Messing Up DI
www.yireo.com - slides.yireo.com
My talkMy talk
Dependency Injection
ObjectManager & Factory
Interfaces
Messing Up DI
www.yireo.com - slides.yireo.com
About meAbout me
Jisse Reitsma
Founder and lead developer of Yireo
Trainer, enterpreneur, coder
Wrote 2 developer books for J**ml*
Loving Magento 2
I live near a palace
Messing Up DI
www.yireo.com - slides.yireo.com
Dependency Injection (DI)Dependency Injection (DI)
Messing Up DI
www.yireo.com - slides.yireo.com
My talkMy talk
Dependency Injection
ObjectManager & Factory
Interfaces
Messing Up DI
www.yireo.com - slides.yireo.com
DI in Magento 2DI in Magento 2
Constructor based DI
Messing Up DI
www.yireo.com - slides.yireo.com
Dependency Injection (DI)Dependency Injection (DI)
Messing Up DI
www.yireo.com - slides.yireo.com
DI in Magento 2DI in Magento 2
Constructor based DI
Inversion of Control
Messing Up DI
www.yireo.com - slides.yireo.com
DI in Magento 2DI in Magento 2
Constructor based DI
Messing Up DI
www.yireo.com - slides.yireo.com
DI in Magento 2DI in Magento 2
Constructor based DI
Inversion of Control
Hollywood Principle
Don't call us, we call you
Messing Up DI
www.yireo.com - slides.yireo.com
DI in Magento 2DI in Magento 2
Constructor based DI
Inversion of Control
Messing Up DI
www.yireo.com - slides.yireo.com
DI in Magento 2DI in Magento 2
Constructor based DI
Inversion of Control
Hollywood Principle
Don't call us, we call you
ObjectManager is our new god
Messing Up DI
www.yireo.com - slides.yireo.com
DI in Magento 2DI in Magento 2
Constructor based DI
Inversion of Control
Hollywood Principle
Don't call us, we call you
Messing Up DI
www.yireo.com - slides.yireo.com
DI in Magento 2DI in Magento 2
Constructor based DI
Inversion of Control
Hollywood Principle
Don't call us, we call you
ObjectManager is our new god
Code smells are everywhere
Messing Up DI
www.yireo.com - slides.yireo.com
DI in Magento 2DI in Magento 2
Constructor based DI
Inversion of Control
Hollywood Principle
Don't call us, we call you
ObjectManager is our new god
Messing Up DI
www.yireo.com - slides.yireo.com
DI in theoryDI in theorynamespace Yireo\Example\Helper;use Magento\Framework\Logger\Monolog;
class Data{ protected $logger;
public function __construct(Monolog $logger) { $this->logger = $logger; }
public function log($text) { // @todo: Do something with $this->logger }}
Messing Up DI
www.yireo.com - slides.yireo.com
DI in Magento 2DI in Magento 2
Constructor based DI
Inversion of Control
Hollywood Principle
Don't call us, we call you
ObjectManager is our new god
Code smells are everywhere
Messing Up DI
www.yireo.com - slides.yireo.com
DI in practiceDI in practicenamespace Yireo\Example\Helper;use Magento\Framework\Logger\Monolog;use Magento\Framework\App\Helper\AbstractHelper;use Magento\Framework\App\Helper\Context;
class Data extends AbstractHelper{ protected $logger;
public function __construct( Monolog $logger Context $context ) { $this->logger = $logger; parent::construct($context); }
...}
Messing Up DI
www.yireo.com - slides.yireo.com
DI in theoryDI in theorynamespace Yireo\Example\Helper;use Magento\Framework\Logger\Monolog;
class Data{ protected $logger;
public function __construct(Monolog $logger) { $this->logger = $logger; }
public function log($text) { // @todo: Do something with $this->logger }}
Messing Up DI
www.yireo.com - slides.yireo.com
Too many dependenciesToo many dependenciesnamespace Magento\Catalog\Model;use ...;
class Product extends \Magento\Catalog\Model\AbstractModel implements IdentityInterface, SaleableInterface, ProductInterface{ public function __construct( \Magento\Framework\Model\Context $context, \Magento\Framework\Registry $registry, \Magento\Framework\Api\ExtensionAttributesFactory $extensionFactory AttributeValueFactory $customAttributeFactory, \Magento\Store\Model\StoreManagerInterface $storeManager, \Magento\Catalog\Api\ProductAttributeRepositoryInterface $metadataService Product\Url $url, Product\Link $productLink, \Magento\Catalog\Model\Product\Configuration\Item\OptionFactory $itemOptionFactory ... ) { ...
Messing Up DI
www.yireo.com - slides.yireo.com
DI in practiceDI in practicenamespace Yireo\Example\Helper;use Magento\Framework\Logger\Monolog;use Magento\Framework\App\Helper\AbstractHelper;use Magento\Framework\App\Helper\Context;
class Data extends AbstractHelper{ protected $logger;
public function __construct( Monolog $logger Context $context ) { $this->logger = $logger; parent::construct($context); }
...}
Messing Up DI
www.yireo.com - slides.yireo.com
Too many dependenciesToo many dependencies
Magento\Catalog\Model\Product has 35
constructor arguments
Messing Up DI
www.yireo.com - slides.yireo.com
Too many dependenciesToo many dependenciesnamespace Magento\Catalog\Model;use ...;
class Product extends \Magento\Catalog\Model\AbstractModel implements IdentityInterface, SaleableInterface, ProductInterface{ public function __construct( \Magento\Framework\Model\Context $context, \Magento\Framework\Registry $registry, \Magento\Framework\Api\ExtensionAttributesFactory $extensionFactory AttributeValueFactory $customAttributeFactory, \Magento\Store\Model\StoreManagerInterface $storeManager, \Magento\Catalog\Api\ProductAttributeRepositoryInterface $metadataService Product\Url $url, Product\Link $productLink, \Magento\Catalog\Model\Product\Configuration\Item\OptionFactory $itemOptionFactory ... ) { ...
Messing Up DI
www.yireo.com - slides.yireo.com
Too many dependenciesToo many dependencies
Magento\Catalog\Model\Product has 35
constructor arguments
One of these constructor arguments is $context
Messing Up DI
www.yireo.com - slides.yireo.com
Too many dependenciesToo many dependencies
Magento\Catalog\Model\Product has 35
constructor arguments
Messing Up DI
www.yireo.com - slides.yireo.com
Discovering $contextDiscovering $contextnamespace Magento\Framework\App\Helper;
abstract class Helper{ public function __construct(Context $context) { $this->_moduleManager = $context->getModuleManager(); $this->_logger = $context->getLogger(); $this->_request = $context->getRequest(); $this->_urlBuilder = $context->getUrlBuilder(); $this->_httpHeader = $context->getHttpHeader(); $this->_eventManager = $context->getEventManager(); $this->_remoteAddress = $context->getRemoteAddress(); $this->_cacheConfig = $context->getCacheConfig(); $this->urlEncoder = $context->getUrlEncoder(); $this->urlDecoder = $context->getUrlDecoder(); $this->scopeConfig = $context->getScopeConfig(); }}
Messing Up DI
www.yireo.com - slides.yireo.com
Too many dependenciesToo many dependencies
Magento\Catalog\Model\Product has 35
constructor arguments
One of these constructor arguments is $context
Messing Up DI
www.yireo.com - slides.yireo.com
Too many dependenciesToo many dependencies
Magento\Catalog\Model\Product has 35
constructor arguments
One of these constructor arguments is $context
Magento\Framework\Model\Context contains another 20+
dependencies
Messing Up DI
www.yireo.com - slides.yireo.com
Discovering $contextDiscovering $contextnamespace Magento\Framework\App\Helper;
abstract class Helper{ public function __construct(Context $context) { $this->_moduleManager = $context->getModuleManager(); $this->_logger = $context->getLogger(); $this->_request = $context->getRequest(); $this->_urlBuilder = $context->getUrlBuilder(); $this->_httpHeader = $context->getHttpHeader(); $this->_eventManager = $context->getEventManager(); $this->_remoteAddress = $context->getRemoteAddress(); $this->_cacheConfig = $context->getCacheConfig(); $this->urlEncoder = $context->getUrlEncoder(); $this->urlDecoder = $context->getUrlDecoder(); $this->scopeConfig = $context->getScopeConfig(); }}
Messing Up DI
www.yireo.com - slides.yireo.com
Too many dependenciesToo many dependencies
Magento\Catalog\Model\Product has 35
constructor arguments
One of these constructor arguments is $context
Magento\Framework\Model\Context contains another 20+
dependencies
What is the meaning of an underscore?
Both $this->_logger and $this->urlEncoder are protected
Messing Up DI
www.yireo.com - slides.yireo.com
Too many dependenciesToo many dependencies
Magento\Catalog\Model\Product has 35
constructor arguments
One of these constructor arguments is $context
Magento\Framework\Model\Context contains another 20+
dependencies
Messing Up DI
www.yireo.com - slides.yireo.com
ObjectManager & FactoryObjectManager & Factory
Messing Up DI
www.yireo.com - slides.yireo.com
Too many dependenciesToo many dependencies
Magento\Catalog\Model\Product has 35
constructor arguments
One of these constructor arguments is $context
Magento\Framework\Model\Context contains another 20+
dependencies
What is the meaning of an underscore?
Both $this->_logger and $this->urlEncoder are protected
Messing Up DI
www.yireo.com - slides.yireo.com
Using DIUsing DI
Requires object to be injectable
Messing Up DI
www.yireo.com - slides.yireo.com
ObjectManager & FactoryObjectManager & Factory
Messing Up DI
www.yireo.com - slides.yireo.com
Using DIUsing DI
Requires object to be injectable
Non-injectables can't be injected via DI
Messing Up DI
www.yireo.com - slides.yireo.com
Using DIUsing DI
Requires object to be injectable
Messing Up DI
www.yireo.com - slides.yireo.com
Using DIUsing DI
Requires object to be injectable
Non-injectables can't be injected via DI
Non-injectables can be instantiated by ObjectManager
Messing Up DI
www.yireo.com - slides.yireo.com
Using DIUsing DI
Requires object to be injectable
Non-injectables can't be injected via DI
Messing Up DI
www.yireo.com - slides.yireo.com
Using DIUsing DI
Requires object to be injectable
Non-injectables can't be injected via DI
Non-injectables can be instantiated by ObjectManager
Solution: Use a Factory
Messing Up DI
www.yireo.com - slides.yireo.com
Using DIUsing DI
Requires object to be injectable
Non-injectables can't be injected via DI
Non-injectables can be instantiated by ObjectManager
Messing Up DI
www.yireo.com - slides.yireo.com
FactoryFactorynamespace Magento\Core\Model\Config;use Magento\Framework\ObjectManager;class BaseFactory{ protected $_objectManager; public function __construct(ObjectManager $objectManager) { $this->_objectManager = $objectManager; } public function create($sourceData = null) { return $this->_objectManager->create( 'Magento\Core\Model\Config\Base', array('sourceData' => $sourceData)); }}
Messing Up DI
www.yireo.com - slides.yireo.com
Using DIUsing DI
Requires object to be injectable
Non-injectables can't be injected via DI
Non-injectables can be instantiated by ObjectManager
Solution: Use a Factory
Messing Up DI
www.yireo.com - slides.yireo.com
Rules on instantiationRules on instantiation
Object Manager should NEVER be used
Messing Up DI
www.yireo.com - slides.yireo.com
FactoryFactorynamespace Magento\Core\Model\Config;use Magento\Framework\ObjectManager;class BaseFactory{ protected $_objectManager; public function __construct(ObjectManager $objectManager) { $this->_objectManager = $objectManager; } public function create($sourceData = null) { return $this->_objectManager->create( 'Magento\Core\Model\Config\Base', array('sourceData' => $sourceData)); }}
Messing Up DI
www.yireo.com - slides.yireo.com
Except for in a Factory
Messing Up DI
www.yireo.com - slides.yireo.com
Rules on instantiationRules on instantiation
Object Manager should NEVER be used
Messing Up DI
www.yireo.com - slides.yireo.com
And CLI scripts
Messing Up DI
www.yireo.com - slides.yireo.com
Except for in a Factory
Messing Up DI
www.yireo.com - slides.yireo.com
InterfacesInterfaces
Messing Up DI
www.yireo.com - slides.yireo.com
And CLI scripts
Messing Up DI
www.yireo.com - slides.yireo.com
DI in theoryDI in theory```php namespace Yireo\Example\Helper; useMagento\Framework\Logger\Monolog;
class Data { protected $logger;
public function __construct(Monolog $logger){ $this->logger = $logger;}
public function log($text){ // @todo: Do something with $this->logger}
}
Messing Up DI
www.yireo.com - slides.yireo.com
InterfacesInterfaces
Messing Up DI
www.yireo.com - slides.yireo.com
DI in theoryDI in theory```php namespace Yireo\Example\Helper; useMagento\Framework\Logger\Monolog;
class Data { protected $logger;
public function __construct(Monolog $logger){ $this->logger = $logger;}
public function log($text){ // @todo: Do something with $this->logger}
}
Messing Up DI
www.yireo.com - slides.yireo.com
DI in theoryDI in theory```php namespace Yireo\Example\Helper; useMagento\Framework\Logger\Monolog;
class Data { protected $logger;
public function __construct(Monolog $logger){ $this->logger = $logger;}
public function log($text){ // @todo: Do something with $this->logger}
}
Messing Up DI
www.yireo.com - slides.yireo.com
DI in theoryDI in theory```php namespace Yireo\Example\Helper; useMagento\Framework\Logger\Monolog;
class Data { protected $logger;
public function __construct(Monolog $logger){ $this->logger = $logger;}
public function log($text){ // @todo: Do something with $this->logger}
}
Messing Up DI
www.yireo.com - slides.yireo.com
DI in theoryDI in theory```php namespace Yireo\Example\Helper; useMagento\Framework\Logger\Monolog;
class Data { protected $logger;
public function __construct(Monolog $logger){ $this->logger = $logger;}
public function log($text){ // @todo: Do something with $this->logger}
}
Messing Up DI
www.yireo.com - slides.yireo.com
ConclusionConclusion
Messing Up DI
www.yireo.com - slides.yireo.com
DI in theoryDI in theory```php namespace Yireo\Example\Helper; useMagento\Framework\Logger\Monolog;
class Data { protected $logger;
public function __construct(Monolog $logger){ $this->logger = $logger;}
public function log($text){ // @todo: Do something with $this->logger}
}
Messing Up DI
www.yireo.com - slides.yireo.com
ConclusionConclusion
Magento 2 is great
Messing Up DI
www.yireo.com - slides.yireo.com
ConclusionConclusion
Messing Up DI
www.yireo.com - slides.yireo.com
ConclusionConclusion
Magento 2 is great
Magento 2 is made by humans
Messing Up DI
www.yireo.com - slides.yireo.com
ConclusionConclusion
Magento 2 is great
Messing Up DI
www.yireo.com - slides.yireo.com
EndEnd
Messing Up DI
www.yireo.com - slides.yireo.com
ConclusionConclusion
Magento 2 is great
Magento 2 is made by humans
Messing Up DI
www.yireo.com - slides.yireo.com
EndEnd
Messing Up DI
www.yireo.com - slides.yireo.com