PHP (83%)JavaScript (10%)Python (1%)VimL (1%)C# (1%)Shell (1%)@gonzalo123http://gonzalo123.comhttps://github.com/gonzalo12...
http://gonzalo123.comhttp://katayunos.comYou can find me at ...Desymfony 2013 Madrid @gonzalo123 http://gonzalo123.com
Why should we useDependency Injection?Desymfony 2013 Madrid @gonzalo123 http://gonzalo123.com
S Single responsibilityO Open/closed principleL Liskov substitutionI Interface segregationD Dependency inversion principle...
IoC and TDDDependency injection is a requirementif you want to write testable code.Desymfony 2013 Madrid @gonzalo123 http:...
class Order{public function generateOrderForUser($user){$backend = new BackendOrder(dsn:...);$orderId = $backend->createOr...
class Order{public function generateOrderForUser($user){$backend = new BackendOrder(dsn:...);$orderId = $backend->createOr...
class Order{public function generateOrderForUser($user){$backend = new BackendOrder(dsn:...);$orderId = $backend->createOr...
class Order{public function generateOrderForUser($user){$backend = new BackendOrder(dsn:...);$orderId = $backend->createOr...
class Order{public function generateOrderForUser($user){$backend = new BackendOrder(dsn:...);$orderId = $backend->createOr...
class Order{private $backend, $pdf, $mailer;public function __construct(BackendOrder $backend, PDFGenerator $pdf, Mailer$m...
class Order{private $backend, $pdf, $mailer;public function __construct(BackendOrder $backend, PDFGenerator $pdf, Mailer$m...
The Hollywood principle"Dont call us,well call you."Desymfony 2013 Madrid @gonzalo123 http://gonzalo123.com
class Order{private $backend, $pdf, $mailer;public function __construct(BackendOrder $backend, PDFGenerator $pdf,Mailer $m...
class Order{private $backend, $pdf, $mailer;public function __construct(BackendOrder $backend, PDFGenerator $pdf,Mailer $m...
Dependency Injection Containers (DIC)to the rescue!Desymfony 2013 Madrid @gonzalo123 http://gonzalo123.com
Dependency Injection Containers (DIC)in PHP? Are u crazy?Desymfony 2013 Madrid @gonzalo123 http://gonzalo123.com
Yes! DIC in PHP:Desymfony 2013 Madrid @gonzalo123 http://gonzalo123.com
<?phpclass Container {protected $s=array();function __set($k, $c) { $this->s[$k]=$c; }function __get($k) { return $this->s...
$c = new Container();$c->backend_driver = function () { return dsn:...; };$c->backend = function ($c) {return new BackendO...
$c = new Pimple();$c[backend_driver] = mysql:...;$c[backend] = function ($c) {return new BackendOrder($c[backend_driver]);...
class MyContainer extends Pimple{public function __construct(){$this[backend_driver] = dsn:...;$c[backend] = function ($th...
composer.json{"require": {"symfony/dependency-injection": "v2.3.0","symfony/yaml": "v2.3.0","symfony/config": "v2.3.0"}}Sy...
services.ymlparameters:backend.driver: dsn:...mailer.credentials: mailCredentials...services:backend:class: BackendOrderar...
index.phpuse SymfonyComponentDependencyInjectionContainerBuilder;use SymfonyComponentConfigFileLocator;use SymfonyComponen...
● Symfony2 (SymfonyDependencyInjection)● Zend Framework2 (ZendDi)● Silex (Pimple)● Laravel (IlluminateContainer)Modern fra...
Usar contenedores o no usarlosUse an IoC container wisely, notpromiscuouslyTo use or not to use DICDesymfony 2013 Madrid @...
Lets code!Desymfony 2013 Madrid @gonzalo123 http://gonzalo123.com
services.ymlparameters:twig.cache: /path/to/cache/pathtwig.auto_reload: truedb.dsn: sqlite::memorydb.username: usernamedb....
services.ymlimports:- resource: twig.yml- resource: database.ymltwig.ymlparameters:twig.cache: /path/to/cache/pathtwig.aut...
require_once __DIR__./vendor/autoload.php;$app = new SilexApplication();$app->get(/hello/{name}, function($name) {return H...
// services.ymlservices:capitalizer:class: Capitalizeruse SymfonyComponentDependencyInjectionContainerBuilder;use SymfonyC...
Almost the end ...● Be decoupled my friend● S.O.L.I.D.● DI is good● DIC are tools to help us.● PHP has different DICs● Sym...
Thank you! - ¡Gracias! - ¡Eskerrik asko!Your turn. Questions?Desymfony 2013 Madrid @gonzalo123 http://gonzalo123.com
http://gonzalo123.comgonzalo123http://joind.in/talk/view/8843
Desymfony2013.gonzalo123
Desymfony2013.gonzalo123
Upcoming SlideShare
Loading in …5
×

Desymfony2013.gonzalo123

5,973 views

Published on

Mi charla sobre Dependencias y Contenedores de inyección en la conferencia deSymfony 2013

0 Comments
3 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
5,973
On SlideShare
0
From Embeds
0
Number of Embeds
4,763
Actions
Shares
0
Downloads
46
Comments
0
Likes
3
Embeds 0
No embeds

No notes for slide

Desymfony2013.gonzalo123

  1. 1. PHP (83%)JavaScript (10%)Python (1%)VimL (1%)C# (1%)Shell (1%)@gonzalo123http://gonzalo123.comhttps://github.com/gonzalo123http://www.linkedin.com/in/gonzaloayusoHello my name is ...Gonzalo Ayuso(according to github)Desymfony 2013 Madrid @gonzalo123 http://gonzalo123.com
  2. 2. http://gonzalo123.comhttp://katayunos.comYou can find me at ...Desymfony 2013 Madrid @gonzalo123 http://gonzalo123.com
  3. 3. Why should we useDependency Injection?Desymfony 2013 Madrid @gonzalo123 http://gonzalo123.com
  4. 4. S Single responsibilityO Open/closed principleL Liskov substitutionI Interface segregationD Dependency inversion principleS.O.L.I.D.Desymfony 2013 Madrid @gonzalo123 http://gonzalo123.com
  5. 5. IoC and TDDDependency injection is a requirementif you want to write testable code.Desymfony 2013 Madrid @gonzalo123 http://gonzalo123.com
  6. 6. class Order{public function generateOrderForUser($user){$backend = new BackendOrder(dsn:...);$orderId = $backend->createOrder();$pdfGenerator = new PDFGenerator();$pdf = $pdfGenerator->getPDF($orderId);$mailer = new Mailer(mailCredentials...);$mailer->sendPdfToUSer($pdf, $user);}}$order = new Order();$order->generateOrderForUser(Gonzalo);One example. It works but ...Desymfony 2013 Madrid @gonzalo123 http://gonzalo123.com
  7. 7. class Order{public function generateOrderForUser($user){$backend = new BackendOrder(dsn:...);$orderId = $backend->createOrder();$pdfGenerator = new PDFGenerator();$pdf = $pdfGenerator->getPDF($orderId);$mailer = new Mailer(mailCredentials...);$mailer->sendPdfToUSer($pdf, $user);}}$order = new Order();$order->generateOrderForUser(Gonzalo);Be decoupled my friendDesymfony 2013 Madrid @gonzalo123 http://gonzalo123.com
  8. 8. class Order{public function generateOrderForUser($user){$backend = new BackendOrder(dsn:...);$orderId = $backend->createOrder();$pdfGenerator = new PDFGenerator();$pdf = $pdfGenerator->getPDF($orderId);$mailer = new Mailer(mailCredentials...);$mailer->sendPdfToUSer($pdf, $user);}}$order = new Order();$order->generateOrderForUser(Gonzalo);Magic strings are evilDesymfony 2013 Madrid @gonzalo123 http://gonzalo123.com
  9. 9. class Order{public function generateOrderForUser($user){$backend = new BackendOrder(dsn:...);$orderId = $backend->createOrder();$pdfGenerator = new PDFGenerator();$pdf = $pdfGenerator->getPDF($orderId);$mailer = new Mailer(mailCredentials...);$mailer->sendPdfToUSer($pdf, $user);}}$order = new Order();$order->generateOrderForUser(Gonzalo);Can we change anything?Desymfony 2013 Madrid @gonzalo123 http://gonzalo123.com
  10. 10. class Order{public function generateOrderForUser($user){$backend = new BackendOrder(dsn:...);$orderId = $backend->createOrder();$pdfGenerator = new PDFGenerator();$pdf = $pdfGenerator->getPDF($orderId);// remember to remove this comment when we deploy//$mailer = new Mailer(mailCredentials...);//$mailer->sendPdfToUSer($pdf, $user);}}$order = new Order();$order->generateOrderForUser(Gonzalo);Can we change anything?Open/Close PrincipleDesymfony 2013. Madrid @gonzalo123 http://gonzalo123.com
  11. 11. class Order{private $backend, $pdf, $mailer;public function __construct(BackendOrder $backend, PDFGenerator $pdf, Mailer$mailer){$this->backend = $backend;$this->pdf = $pdf;$this->mailer = $mailer;}public function generateOrderForUser($user){$orderId = $this->backend->createOrder();$pdf = $this->pdfGenerator->getPDF($orderId);$this->mailer->sendPdfToUSer($pdf, $user);}}$backend = new BackendOrder(dsn:...);$pdfGenerator = new PDFGenerator();$mailer = new Mailer(mailCredentials...);$order = new Order($backend, $pdfGenerator, $mailer);$order->generateOrderForUser(Gonzalo);Refactor -> DIDesymfony 2013 Madrid @gonzalo123 http://gonzalo123.com
  12. 12. class Order{private $backend, $pdf, $mailer;public function __construct(BackendOrder $backend, PDFGenerator $pdf, Mailer$mailer){$this->backend = $backend;$this->pdf = $pdf;$this->mailer = $mailer;}public function generateOrderForUser($user){$orderId = $this->backend->createOrder();$pdf = $this->pdfGenerator->getPDF($orderId);$this->mailer->sendPdfToUSer($pdf, $user);}}$backend = new BackendOrder(dsn:...);$pdfGenerator = new PDFGenerator();$mailer = new Mailer(mailCredentials...);$order = new Order($backend, $pdfGenerator, $mailer);$order->generateOrderForUser(Gonzalo);Refactor -> DIAndthatsall folks!Desymfony 2013 Madrid @gonzalo123 http://gonzalo123.com
  13. 13. The Hollywood principle"Dont call us,well call you."Desymfony 2013 Madrid @gonzalo123 http://gonzalo123.com
  14. 14. class Order{private $backend, $pdf, $mailer;public function __construct(BackendOrder $backend, PDFGenerator $pdf,Mailer $mailer){$this->backend = $backend;$this->pdf = $pdf;$this->mailer = $mailer;}public function generateOrderForUser($user){$orderId = $this->backend->createOrder();$pdf = $this->pdfGenerator->getPDF($orderId);$this->mailer->sendPdfToUSer($pdf, $user);}}$backend = new BackendOrder(dsn:...);$pdfGenerator = new PDFGenerator();$mailer = new Mailer(mailCredentials...);$order = new Order($backend, $pdfGenerator, $mailer);$order->generateOrderForUser(Gonzalo);Where is the problem now?Desymfony 2013 Madrid @gonzalo123 http://gonzalo123.com
  15. 15. class Order{private $backend, $pdf, $mailer;public function __construct(BackendOrder $backend, PDFGenerator $pdf,Mailer $mailer){$this->backend = $backend;$this->pdf = $pdf;$this->mailer = $mailer;}public function generateOrderForUser($user){$orderId = $this->backend->createOrder();$pdf = $this->pdfGenerator->getPDF($orderId);$this->mailer->sendPdfToUSer($pdf, $user);}}$backend = new BackendOrder(dsn:...);$pdfGenerator = new PDFGenerator();$mailer = new Mailer(mailCredentials...);$order = new Order($backend, $pdfGenerator, $mailer);$order->generateOrderForUser(Gonzalo);What can we do?Desymfony 2013 Madrid @gonzalo123 http://gonzalo123.com
  16. 16. Dependency Injection Containers (DIC)to the rescue!Desymfony 2013 Madrid @gonzalo123 http://gonzalo123.com
  17. 17. Dependency Injection Containers (DIC)in PHP? Are u crazy?Desymfony 2013 Madrid @gonzalo123 http://gonzalo123.com
  18. 18. Yes! DIC in PHP:Desymfony 2013 Madrid @gonzalo123 http://gonzalo123.com
  19. 19. <?phpclass Container {protected $s=array();function __set($k, $c) { $this->s[$k]=$c; }function __get($k) { return $this->s[$k]($this); }}TwitteA Dependency Injection Container in a TweetDesymfony 2013 Madrid @gonzalo123 http://gonzalo123.com
  20. 20. $c = new Container();$c->backend_driver = function () { return dsn:...; };$c->backend = function ($c) {return new BackendOrder($c->backend_driver);};$c->pdfGenerator = function ($c) {return new PDFGenerator();};$c->mailer_credentials = function () { returnmailCredentials...; };$c->mailer = function ($c) {return new Mailer($c->mailer_credentials);};$c->order = function ($c) {return new Order($c->backend, $c->pdfGenerator, $c->mailer);};$order = $c->order;$order->generateOrderForUser(Gonzalo);TwitteA Dependency Injection Container in a TweetDesymfony 2013 Madrid @gonzalo123 http://gonzalo123.com
  21. 21. $c = new Pimple();$c[backend_driver] = mysql:...;$c[backend] = function ($c) {return new BackendOrder($c[backend_driver]);};$c[pdfGenerator] = function ($c) {return new PDFGenerator();};$c[mailer_credentials] = mailCredentials...;$c[mailer] = function ($c) {return new Mailer($c[mailer_credentials]);};$c[order] = function ($c) {return new Order($c[backend], $c[pdfGenerator], $c[mailer]);};$order = $c[order];$order->generateOrderForUser(Gonzalo);PimpleA Simple Dependency Injection Container for PHP 5.3Desymfony 2013 Madrid @gonzalo123 http://gonzalo123.com
  22. 22. class MyContainer extends Pimple{public function __construct(){$this[backend_driver] = dsn:...;$c[backend] = function ($this) {return new BackendOrder($this[backend_driver]);};$c[pdfGenerator] = function ($c) {return new PDFGenerator();};$this[mailer_credentials] = mailCredentials...;$this[mailer] = function ($this) {return new Mailer($this[mailer_credentials]);};$this[order] = function ($this) {return new Order($this[backend], $this[pdfGenerator], $this[mailer]);};}}$c = new MyContainer();$order = $c[order];$order->generateOrderForUser(Gonzalo);PimpleA Simple Dependency Injection Container for PHP 5.3Desymfony 2013 Madrid @gonzalo123 http://gonzalo123.com
  23. 23. composer.json{"require": {"symfony/dependency-injection": "v2.3.0","symfony/yaml": "v2.3.0","symfony/config": "v2.3.0"}}Symfony DICDesymfony 2013 Madrid @gonzalo123 http://gonzalo123.com
  24. 24. services.ymlparameters:backend.driver: dsn:...mailer.credentials: mailCredentials...services:backend:class: BackendOrderarguments: [%backend.driver%]pdfGenerator:class: PDFGeneratormailer:class: Mailerarguments: [%mailer.credentials%]order:class: Orderarguments: [@backend, @pdfGenerator,@mailer]Symfony DICDesymfony 2013 Madrid @gonzalo123 http://gonzalo123.com
  25. 25. index.phpuse SymfonyComponentDependencyInjectionContainerBuilder;use SymfonyComponentConfigFileLocator;use SymfonyComponentDependencyInjectionLoaderYamlFileLoader;$container = new ContainerBuilder();$loader = new YamlFileLoader($container, new FileLocator(__DIR__));$loader->load(services.yml);$order = $container->get(order);$order->generateOrderForUser(Gonzalo);Symfony DICDesymfony 2013 Madrid @gonzalo123 http://gonzalo123.com
  26. 26. ● Symfony2 (SymfonyDependencyInjection)● Zend Framework2 (ZendDi)● Silex (Pimple)● Laravel (IlluminateContainer)Modern frameworks and DICDesymfony 2013 Madrid @gonzalo123 http://gonzalo123.com
  27. 27. Usar contenedores o no usarlosUse an IoC container wisely, notpromiscuouslyTo use or not to use DICDesymfony 2013 Madrid @gonzalo123 http://gonzalo123.com
  28. 28. Lets code!Desymfony 2013 Madrid @gonzalo123 http://gonzalo123.com
  29. 29. services.ymlparameters:twig.cache: /path/to/cache/pathtwig.auto_reload: truedb.dsn: sqlite::memorydb.username: usernamedb.password: passwordservices:database:class: PDOarguments: [%db.dsn%, %db.username%, %db.password%]Scaling our services.ymlDesymfony 2013 Madrid @gonzalo123 http://gonzalo123.com
  30. 30. services.ymlimports:- resource: twig.yml- resource: database.ymltwig.ymlparameters:twig.cache: /path/to/cache/pathtwig.auto_reload: truedatabase.ymlparameters:db.dsn: sqlite::memorydb.username: usernamedb.password: passwordservices:database:class: PDOarguments: [%db.dsn%, %db.username%, %db.password%, ]Scaling our services.ymlDesymfony 2013 Madrid @gonzalo123 http://gonzalo123.com
  31. 31. require_once __DIR__./vendor/autoload.php;$app = new SilexApplication();$app->get(/hello/{name}, function($name) {return Hello .strtoupper($name);});$app->run();Silex exampleDesymfony 2013 Madrid @gonzalo123 http://gonzalo123.com
  32. 32. // services.ymlservices:capitalizer:class: Capitalizeruse SymfonyComponentDependencyInjectionContainerBuilder;use SymfonyComponentConfigFileLocator;use SymfonyComponentDependencyInjectionLoaderYamlFileLoader;$container = new ContainerBuilder();$loader = new YamlFileLoader($container, new FileLocator(__DIR__));$loader->load(services.yml);$app = new SilexApplication();$app[container] = $container;$app->get(/hello/{name}, function($name) use($app) {$capitalizer = $app[container]->get(capitalizer);return Hello . $capitalizer->toUpper($name);});$app->run();Silex example with DICDesymfony 2013 Madrid @gonzalo123 http://gonzalo123.com
  33. 33. Almost the end ...● Be decoupled my friend● S.O.L.I.D.● DI is good● DIC are tools to help us.● PHP has different DICs● Symfony DIC is veryeasy to implement,configurable and easyto extendDesymfony 2013 Madrid @gonzalo123 http://gonzalo123.com
  34. 34. Thank you! - ¡Gracias! - ¡Eskerrik asko!Your turn. Questions?Desymfony 2013 Madrid @gonzalo123 http://gonzalo123.com
  35. 35. http://gonzalo123.comgonzalo123http://joind.in/talk/view/8843

×