Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Dependency Injection in Laravel

1,491 views

Published on

Slide of talk in [PHP 也有 Day]

Published in: Software
  • Can you write this in English as well?
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here

Dependency Injection in Laravel

  1. 1. Dependency Inversion Dependency Injection Contract SOLID Service Provider Service Container IoC DI
  2. 2. Dependency Inversion SOLID High-level modules should not depend on low-level modules. Both should depend on abstractions. http://en.wikipedia.org/wiki/Dependency_inversion_principle
  3. 3. ⾼高階模組不應該依賴低階模組。 兩者都應該要依賴於抽象 What the …
  4. 4. class UserModify { public function __constructor() { $this->storage = new MysqlStorage(); } public function modify($data) { $result = $this->storage->save($data); return $result; } } ⾼高階直接依賴低階
  5. 5. class UserModify { public function __constructor(DataStorageInterface $storage) { $this->storage = $storage; } public function modify($data) { $result = $this->storage->save($data); return $result; } } class MysqlStorage implements DataStorageInterface { public function save($data){ // …. } } interface DataStorageInterface { public function save(array $data); } Abstraction Low Level Module Hight Level Module
  6. 6. Why
  7. 7. #隱藏低階模組的細節 #容易抽換實作 #程式碼變多 #追蹤⽐比較困難 #讓測試變容易 #⽐比較不直覺
  8. 8. 隱藏低階模組的細節 class UserModify { public function __constructor() { $dbConfig = [….]; $this->storage = new MysqlStorage($dbConfig); } }
  9. 9. 容易抽換實作 class UserModify { public function __constructor(DataStorageInterface $storage) { $this->storage = $storage; } } class SqliteStorage implements DataStorageInterface { public function save($data){ // …. } } SqliteStorage Instance
  10. 10. 讓測試變容易 class DummyStorage implements DataStorageInterface { public function save() { return ‘Success’; } } class UserModify { public function modify($data) { return $this->storage->save($data); } } class UserModifyTest { public function it_return_new_instance_after_modify($data) { $instance = new UserModify(new DummyStorage); $result = $instance->modify(); assert($result, ‘Success’); } }
  11. 11. How
  12. 12. Inverse of Control http://zh.wikipedia.org/wiki/%E6%8E%A7%E5%88%B6%E5%8F%8D%E8%BD%AC
  13. 13. Dependency Injection http://en.wikipedia.org/wiki/Dependency_injection Dependency injection is a software design pattern that implements inversion of control for software libraries, where the caller delegates to an external framework the control flow of discovering and importing a service or software module.
  14. 14. Service Provider Container
  15. 15. http://laravel.tw/docs/5.0/providers Service Provider
  16. 16. . ├── AppServiceProvider.php ├── BusServiceProvider.php ├── ConfigServiceProvider.php ├── EventServiceProvider.php ├── FilterServiceProvider.php ├── RepoServiceProvider.php └── RouteServiceProvider.php app/Providers
  17. 17. <?php namespace BackendProviders;
 
 use IlluminateSupportServiceProvider;
 
 class AppServiceProvider extends ServiceProvider {
 
 public function register() { $this->app->bind( ‘Jaster/Interfaces/FooInterface’, ‘Jaster/Implements/FooImplementClass’ ); 
 }
 }
 app/Providers/AppServiceProvider.php
  18. 18. 'providers' => [
 /*
 * Laravel Framework Service Providers...
 */
 'IlluminateFoundationProvidersArtisanServiceProvider',
 'IlluminateAuthAuthServiceProvider',
 'IlluminateBusBusServiceProvider',
 'IlluminateCacheCacheServiceProvider',
 'IlluminateFoundationProvidersConsoleSupportServiceProvider',
 'IlluminateRoutingControllerServiceProvider', // ….
 ], config/app.php
  19. 19. Service Container The Laravel service container is a powerful tool for managing class dependencies. Dependency injection is a fancy word that essentially means this: class dependencies are "injected" into the class via the constructor or, in some cases, "setter" methods. http://laravel.com/docs/5.0/container
  20. 20. Trace initialize of service container ( IlluminateFoundationApplication ) IlluminateFoundationApplication::__construct IlluminateFoundationApplication::registerBaseBindings IlluminateFoundationApplication::registerBaseServiceProviders IlluminateFoundationApplication::registerCoreContainerAliases
  21. 21. Trace register of IlluminateContractsHttpKernel bootstrap/app.php $app->singleton(‘IlluminateContractsHttpKernel', 'AppHttpKernel'); IlluminateFoundationApplication::singleton IlluminateFoundationApplication::bind
  22. 22. Trace how to use IlluminateContractsHttpKernel $kernel = $app->make('IlluminateContractsHttpKernel'); public/index.php IlluminateFoundationApplication::make IlluminateContainerContainer::make IlluminateContainerContainer::build
  23. 23. Trace Facade autoloader IlluminateFoundationHttpKernel::bootstrap IlluminateFoundationApplication::bootstrapWith IlluminateFoundationBootstrapRegisterFacades IlluminateFoundationAliasLoader::register IlluminateFoundationAliasLoader::prependToLoaderStack
  24. 24. PHP UK Conference 2014 - Stephan Hochdorfer - The 7 Deadly Sins of Dependency Injection https://www.youtube.com/watch?v=hMix8gfDy54
  25. 25. Thank you

×