The Flip Side of Dependency Injection · 2019. 9. 4. · The Flip Side of Dependency Injection Arne...

45
The Flip Side of Dependency Injection Arne Blankerts | PHP UG Hamburg | March, 11 th 2014

Transcript of The Flip Side of Dependency Injection · 2019. 9. 4. · The Flip Side of Dependency Injection Arne...

Page 1: The Flip Side of Dependency Injection · 2019. 9. 4. · The Flip Side of Dependency Injection Arne Blankerts | PHP UG Hamburg | March, 11th 2014. This document was created with Prince,

The Flip Side of Dependency InjectionArne Blankerts | PHP UG Hamburg | March, 11th 2014

www.princexml.com
Prince - Non-commercial License
This document was created with Prince, a great way of getting web content onto paper.
Page 2: The Flip Side of Dependency Injection · 2019. 9. 4. · The Flip Side of Dependency Injection Arne Blankerts | PHP UG Hamburg | March, 11th 2014. This document was created with Prince,

» Working with PHP for over a decade(Security paranoid ;) and System Architect)

» Author of phpab and phpDox

» Co-Founder and Consultantwith thePHP.cc

Arne Blankerts

Page 3: The Flip Side of Dependency Injection · 2019. 9. 4. · The Flip Side of Dependency Injection Arne Blankerts | PHP UG Hamburg | March, 11th 2014. This document was created with Prince,

The Problem1 <?php23 classclass Sample {45 privateprivate $backend$backend;6 privateprivate $service$service;78 public functionpublic function __construct() {9 $config$config = newnew Config('some.ini');

10 $this$this->backend = newnew Backend($config$config);11 $this$this->service = newnew SuperService($config$config, 'Sample');12 }1314 }

Page 4: The Flip Side of Dependency Injection · 2019. 9. 4. · The Flip Side of Dependency Injection Arne Blankerts | PHP UG Hamburg | March, 11th 2014. This document was created with Prince,

Solution - Attempt 11 <?php23 classclass Sample {45 privateprivate $backend$backend;6 privateprivate $service$service;78 public functionpublic function __construct() {9 $config$config = Config::getInstance();

10 $this$this->backend = newnew Backend($config$config);11 $this$this->service = newnew SuperService($config$config), 'Sample');12 }1314 }

Page 5: The Flip Side of Dependency Injection · 2019. 9. 4. · The Flip Side of Dependency Injection Arne Blankerts | PHP UG Hamburg | March, 11th 2014. This document was created with Prince,

Singleton?

Page 6: The Flip Side of Dependency Injection · 2019. 9. 4. · The Flip Side of Dependency Injection Arne Blankerts | PHP UG Hamburg | March, 11th 2014. This document was created with Prince,

Singleton? Really?

Page 7: The Flip Side of Dependency Injection · 2019. 9. 4. · The Flip Side of Dependency Injection Arne Blankerts | PHP UG Hamburg | March, 11th 2014. This document was created with Prince,

Solution - Attempt 21 <?php23 classclass Sample {45 privateprivate $backend$backend;6 privateprivate $service$service;78 public functionpublic function __construct(Config $config$config) {9 $this$this->backend = newnew Backend($config$config);

10 $this$this->service = newnew SuperService($config$config), 'Sample');11 }1213 }

Page 8: The Flip Side of Dependency Injection · 2019. 9. 4. · The Flip Side of Dependency Injection Arne Blankerts | PHP UG Hamburg | March, 11th 2014. This document was created with Prince,

The phonebookapproach

Page 9: The Flip Side of Dependency Injection · 2019. 9. 4. · The Flip Side of Dependency Injection Arne Blankerts | PHP UG Hamburg | March, 11th 2014. This document was created with Prince,

Solution - Attempt 31 <?php23 classclass Sample {45 privateprivate $backend$backend;6 privateprivate $service$service;78 public functionpublic function __construct(Config $config$config) {9 $this$this->backend = newnew Backend(

10 $config$config->get('backend.credentials')11 );12 $this$this->service = newnew SuperService(13 $config$config->get('service.hostname')14 );15 }1617 }

Page 10: The Flip Side of Dependency Injection · 2019. 9. 4. · The Flip Side of Dependency Injection Arne Blankerts | PHP UG Hamburg | March, 11th 2014. This document was created with Prince,

Almost there ...

Page 11: The Flip Side of Dependency Injection · 2019. 9. 4. · The Flip Side of Dependency Injection Arne Blankerts | PHP UG Hamburg | March, 11th 2014. This document was created with Prince,

Solution - Dependency Injection?1 <?php23 classclass Sample {45 privateprivate $backend$backend;6 privateprivate $service$service;78 public functionpublic function __construct(Backend $backend$backend, Service $service$service) {9 $this$this->backend = $backend$backend;

10 $this$this->service = $service$service;11 }1213 }

Page 12: The Flip Side of Dependency Injection · 2019. 9. 4. · The Flip Side of Dependency Injection Arne Blankerts | PHP UG Hamburg | March, 11th 2014. This document was created with Prince,

Solution - Dependency Injection!1 <?php23 classclass Sample {45 privateprivate $backend$backend;6 privateprivate $service$service;78 public functionpublic function __construct(9 BackendInterface $backend$backend,

10 ServiceInterface $service$service11 ) {12 $this$this->backend = $backend$backend;13 $this$this->service = $service$service;14 }1516 }

Page 13: The Flip Side of Dependency Injection · 2019. 9. 4. · The Flip Side of Dependency Injection Arne Blankerts | PHP UG Hamburg | March, 11th 2014. This document was created with Prince,
Page 14: The Flip Side of Dependency Injection · 2019. 9. 4. · The Flip Side of Dependency Injection Arne Blankerts | PHP UG Hamburg | March, 11th 2014. This document was created with Prince,

Many dependencies1 <?php23 classclass Sample {45 privateprivate $serviceA$serviceA;6 privateprivate $serviceB$serviceB;7 privateprivate $serviceC$serviceC;8 privateprivate $serviceD$serviceD;9 privateprivate $serviceE$serviceE;

1011 public functionpublic function __construct(12 ServiceAInterface $serviceA$serviceA, ServiceBInterface $serviceB$serviceB,13 ServiceCInterface $serviceC$serviceC, ServiceDInterface $serviceD$serviceD,14 ServiceEInterface $serviceE$serviceE15 ) {16 // [...]17 }1819 }

Page 15: The Flip Side of Dependency Injection · 2019. 9. 4. · The Flip Side of Dependency Injection Arne Blankerts | PHP UG Hamburg | March, 11th 2014. This document was created with Prince,

All-in-one?

Page 16: The Flip Side of Dependency Injection · 2019. 9. 4. · The Flip Side of Dependency Injection Arne Blankerts | PHP UG Hamburg | March, 11th 2014. This document was created with Prince,

Many Dependencies - A Solution?1 <?php23 classclass Sample {45 privateprivate $serviceA$serviceA;6 privateprivate $serviceB$serviceB;7 privateprivate $serviceC$serviceC;8 privateprivate $serviceD$serviceD;9 privateprivate $serviceE$serviceE;

1011 public functionpublic function __construct(Container $container$container) {12 $this$this->serviceA = $container$container->get('serviceA');13 // [...]14 }1516 }

Page 17: The Flip Side of Dependency Injection · 2019. 9. 4. · The Flip Side of Dependency Injection Arne Blankerts | PHP UG Hamburg | March, 11th 2014. This document was created with Prince,

Hidden Dependencies

Page 18: The Flip Side of Dependency Injection · 2019. 9. 4. · The Flip Side of Dependency Injection Arne Blankerts | PHP UG Hamburg | March, 11th 2014. This document was created with Prince,

Back At Square One

Page 19: The Flip Side of Dependency Injection · 2019. 9. 4. · The Flip Side of Dependency Injection Arne Blankerts | PHP UG Hamburg | March, 11th 2014. This document was created with Prince,

Magic?1 <?php23 classclass Sample {45 /**6 * @var ServiceA7 * @Inject8 */9 privateprivate $serviceA$serviceA;

1011 }

Page 20: The Flip Side of Dependency Injection · 2019. 9. 4. · The Flip Side of Dependency Injection Arne Blankerts | PHP UG Hamburg | March, 11th 2014. This document was created with Prince,

Advanced Magic?1 <?php23 classclass Sample {45 /**6 * @var ServiceA7 * @Inject("service.SuperServiceA")8 */9 privateprivate $serviceA$serviceA;

1011 }

Page 21: The Flip Side of Dependency Injection · 2019. 9. 4. · The Flip Side of Dependency Injection Arne Blankerts | PHP UG Hamburg | March, 11th 2014. This document was created with Prince,

Advanced advanced Magic?1 <?php23 classclass Sample {45 /**6 * @var ServiceA7 * @Inject("service.SuperServiceA", Instance="ThatInstance")8 */9 privateprivate $serviceA$serviceA;

1011 }

Page 22: The Flip Side of Dependency Injection · 2019. 9. 4. · The Flip Side of Dependency Injection Arne Blankerts | PHP UG Hamburg | March, 11th 2014. This document was created with Prince,

Extenral Configuration?1 services:2 serviceA:3 classclass: ServiceAImplementor4 serviceB:5 classclass: ServiceBImplementor6 serviceC:7 classclass: ServiceCImplementor

Page 23: The Flip Side of Dependency Injection · 2019. 9. 4. · The Flip Side of Dependency Injection Arne Blankerts | PHP UG Hamburg | March, 11th 2014. This document was created with Prince,

Still At Square One ...

Page 24: The Flip Side of Dependency Injection · 2019. 9. 4. · The Flip Side of Dependency Injection Arne Blankerts | PHP UG Hamburg | March, 11th 2014. This document was created with Prince,

What do we need?

Page 25: The Flip Side of Dependency Injection · 2019. 9. 4. · The Flip Side of Dependency Injection Arne Blankerts | PHP UG Hamburg | March, 11th 2014. This document was created with Prince,

Requirements• All Dependencies must be in code• Seperate Object creation from usage• Ability to choose actual implementation on runtime

Page 26: The Flip Side of Dependency Injection · 2019. 9. 4. · The Flip Side of Dependency Injection Arne Blankerts | PHP UG Hamburg | March, 11th 2014. This document was created with Prince,

Requirements• All Dependencies must be in code

• No hidden Dependencies• No external configuration• No framework magic based on annotations

• Seperate Object Creation from Usage• Ability to choose actual implementation on runtime

Page 27: The Flip Side of Dependency Injection · 2019. 9. 4. · The Flip Side of Dependency Injection Arne Blankerts | PHP UG Hamburg | March, 11th 2014. This document was created with Prince,

Requirements• All Dependencies must be in code• Seperate Object Creation from Usage• Ability to choose actual implementation on runtime

Page 28: The Flip Side of Dependency Injection · 2019. 9. 4. · The Flip Side of Dependency Injection Arne Blankerts | PHP UG Hamburg | March, 11th 2014. This document was created with Prince,

Requirements• All Dependencies must be in code• Seperate Object Creation from Usage• Ability to choose actual Implementation on Runtime

Page 29: The Flip Side of Dependency Injection · 2019. 9. 4. · The Flip Side of Dependency Injection Arne Blankerts | PHP UG Hamburg | March, 11th 2014. This document was created with Prince,

Requirements• All Dependencies must be in code• Seperate Object Creation from Usage• Ability to choose actual Implementation on Runtime

• Run consistent A/B-Tests• Degrade gracefully• Customized execution

Page 30: The Flip Side of Dependency Injection · 2019. 9. 4. · The Flip Side of Dependency Injection Arne Blankerts | PHP UG Hamburg | March, 11th 2014. This document was created with Prince,

How to do that in plain OOP?

Page 31: The Flip Side of Dependency Injection · 2019. 9. 4. · The Flip Side of Dependency Injection Arne Blankerts | PHP UG Hamburg | March, 11th 2014. This document was created with Prince,

Step 1

Page 32: The Flip Side of Dependency Injection · 2019. 9. 4. · The Flip Side of Dependency Injection Arne Blankerts | PHP UG Hamburg | March, 11th 2014. This document was created with Prince,

Factory

Page 33: The Flip Side of Dependency Injection · 2019. 9. 4. · The Flip Side of Dependency Injection Arne Blankerts | PHP UG Hamburg | March, 11th 2014. This document was created with Prince,

1 <?php23 classclass Factory {45 privateprivate $config$config;67 public functionpublic function __construct(Config $config$config) {8 $this$this->config = $config$config;9 }

1011 /**12 * @return ServiceA13 */14 public functionpublic function getServiceA() {15 return newreturn new ServiceA($this$this->getDBConnection());16 }1718 /**19 * @return DBConnection20 */21 private functionprivate function getDBConnection() {22 return newreturn new DBConnection($this$this->config->getDSN());23 }2425 }

Page 34: The Flip Side of Dependency Injection · 2019. 9. 4. · The Flip Side of Dependency Injection Arne Blankerts | PHP UG Hamburg | March, 11th 2014. This document was created with Prince,
Page 35: The Flip Side of Dependency Injection · 2019. 9. 4. · The Flip Side of Dependency Injection Arne Blankerts | PHP UG Hamburg | March, 11th 2014. This document was created with Prince,

Avoid injecting factories!

Page 36: The Flip Side of Dependency Injection · 2019. 9. 4. · The Flip Side of Dependency Injection Arne Blankerts | PHP UG Hamburg | March, 11th 2014. This document was created with Prince,

But what about runtimecomposing?

Page 37: The Flip Side of Dependency Injection · 2019. 9. 4. · The Flip Side of Dependency Injection Arne Blankerts | PHP UG Hamburg | March, 11th 2014. This document was created with Prince,

Locators!

Page 38: The Flip Side of Dependency Injection · 2019. 9. 4. · The Flip Side of Dependency Injection Arne Blankerts | PHP UG Hamburg | March, 11th 2014. This document was created with Prince,

1 <?php23 classclass FooLocator {45 /** @var Factory */6 privateprivate $factory$factory;78 public functionpublic function __construct(Factory $factory$factory) {9 $this$this->factory = $factory$factory;

10 }1112 /**13 * @return FooInterface14 */15 public functionpublic function get($type$type) {16 switchswitch ($type$type) {17 casecase 'a': returnreturn $this$this->factory->getServiceA();18 casecase 'b': returnreturn $this$this->factory->getServiceB();19 defaultdefault: throw newthrow new RuntimeException("Type '$type' not specified");20 }2122 }

Page 39: The Flip Side of Dependency Injection · 2019. 9. 4. · The Flip Side of Dependency Injection Arne Blankerts | PHP UG Hamburg | March, 11th 2014. This document was created with Prince,

That's it?

Page 40: The Flip Side of Dependency Injection · 2019. 9. 4. · The Flip Side of Dependency Injection Arne Blankerts | PHP UG Hamburg | March, 11th 2014. This document was created with Prince,

What happend to A/BTesting?

Page 41: The Flip Side of Dependency Injection · 2019. 9. 4. · The Flip Side of Dependency Injection Arne Blankerts | PHP UG Hamburg | March, 11th 2014. This document was created with Prince,

https://github.com/theseer/Factory

Page 42: The Flip Side of Dependency Injection · 2019. 9. 4. · The Flip Side of Dependency Injection Arne Blankerts | PHP UG Hamburg | March, 11th 2014. This document was created with Prince,

Done?

Page 43: The Flip Side of Dependency Injection · 2019. 9. 4. · The Flip Side of Dependency Injection Arne Blankerts | PHP UG Hamburg | March, 11th 2014. This document was created with Prince,

Done.

Page 45: The Flip Side of Dependency Injection · 2019. 9. 4. · The Flip Side of Dependency Injection Arne Blankerts | PHP UG Hamburg | March, 11th 2014. This document was created with Prince,

sharing experience