jisse reitsma - messing up dependency injection

40
Messing Up Messing Up Dependency Injection Messing Up DI www.yireo.com - slides.yireo.com

Upload: dutchento

Post on 08-Jan-2017

120 views

Category:

Internet


0 download

TRANSCRIPT

Page 1: Jisse Reitsma - Messing up dependency injection

Messing UpMessing Up

Dependency Injection

Messing Up DI

www.yireo.com - slides.yireo.com

Page 2: Jisse Reitsma - Messing up dependency injection

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

Page 3: Jisse Reitsma - Messing up dependency injection

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

Page 4: Jisse Reitsma - Messing up dependency injection

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

Page 5: Jisse Reitsma - Messing up dependency injection

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

Page 6: Jisse Reitsma - Messing up dependency injection

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

Page 7: Jisse Reitsma - Messing up dependency injection

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

Page 8: Jisse Reitsma - Messing up dependency injection

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

Page 9: Jisse Reitsma - Messing up dependency injection

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

Page 10: Jisse Reitsma - Messing up dependency injection

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

Page 11: Jisse Reitsma - Messing up dependency injection

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

Page 12: Jisse Reitsma - Messing up dependency injection

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

Page 13: Jisse Reitsma - Messing up dependency injection

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

Page 14: Jisse Reitsma - Messing up dependency injection

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

Page 15: Jisse Reitsma - Messing up dependency injection

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

Page 16: Jisse Reitsma - Messing up dependency injection

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

Page 17: Jisse Reitsma - Messing up dependency injection

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

Page 18: Jisse Reitsma - Messing up dependency injection

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

Page 19: Jisse Reitsma - Messing up dependency injection

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

Page 20: Jisse Reitsma - Messing up dependency injection

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

Page 21: Jisse Reitsma - Messing up dependency injection

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

Page 22: Jisse Reitsma - Messing up dependency injection

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

Page 23: Jisse Reitsma - Messing up dependency injection

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

Page 24: Jisse Reitsma - Messing up dependency injection

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

Page 25: Jisse Reitsma - Messing up dependency injection

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

Page 26: Jisse Reitsma - Messing up dependency injection

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

Page 27: Jisse Reitsma - Messing up dependency injection

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

Page 28: Jisse Reitsma - Messing up dependency injection

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

Page 29: Jisse Reitsma - Messing up dependency injection

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

Page 30: Jisse Reitsma - Messing up dependency injection

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

Page 31: Jisse Reitsma - Messing up dependency injection

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

Page 32: Jisse Reitsma - Messing up dependency injection

InterfacesInterfaces

Messing Up DI

www.yireo.com - slides.yireo.com

And CLI scripts

Messing Up DI

www.yireo.com - slides.yireo.com

Page 33: Jisse Reitsma - Messing up dependency injection

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

Page 34: Jisse Reitsma - Messing up dependency injection

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

Page 35: Jisse Reitsma - Messing up dependency injection

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

Page 36: Jisse Reitsma - Messing up dependency injection

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

Page 37: Jisse Reitsma - Messing up dependency injection

ConclusionConclusion

Magento 2 is great

Messing Up DI

www.yireo.com - slides.yireo.com

ConclusionConclusion

Messing Up DI

www.yireo.com - slides.yireo.com

Page 38: Jisse Reitsma - Messing up dependency injection

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

Page 39: Jisse Reitsma - Messing up dependency injection

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

Page 40: Jisse Reitsma - Messing up dependency injection

EndEnd

Messing Up DI

www.yireo.com - slides.yireo.com