Slide 1

Slide 1 text

Dependency Injection and the Symfony2 Service Container

Slide 2

Slide 2 text

Richard Miller ● http://www.limethinking.co.uk ● http://miller.limethinking.co.uk ● @mr_r_miller

Slide 3

Slide 3 text

What is Dependency Injection?

Slide 4

Slide 4 text

class ProductController { public function addAction() { //validate the POST variables $productSaver = new ProductSaver(); $productSaver->save($product); } //-- }

Slide 5

Slide 5 text

class ProductSaver { public function save($product) { $mapper = new DataMapper(); $mapper->save($product); $emailNotifier = new EmailNotifier(); $emailNotifier->notify($product); } }

Slide 6

Slide 6 text

class EmailNotifier { public function notify($product) { $mail = new Mail(); $mail->addTo('[email protected]'); $mail->addCC('[email protected]'); //create message body using $product $mailer = new Mailer(); $mailer->send($mail); } }

Slide 7

Slide 7 text

Problems with EmailNotifier ● Not reusable ● Change of email addresses ● Change of mailer ● Impossible to unit test ● Any test will create an actual Mailer and send the email

Slide 8

Slide 8 text

public function notify($product) { $mail = new Mail(); $mail->addTo($this->toAddress); $mail->addCC('[email protected]'); //create message body using $product $mailer = new Mailer(); $mailer->send($mail); } protected $toAddress; public function __construct($toAddress) { $this->toAddress = $toAddress; } $mail->addTo($this->toAddress);

Slide 9

Slide 9 text

protected $toAddress; protected $ccAddresses = array(); public function __construct($toAddress) { $this->toAddress = $toAddress; } public function setCcAddress($ccAddress) { $this->ccAddresses[] = $ccAdresss; } public function notify($product) { $mail = new Mail(); $mail->addTo($this->toAddress); foreach($this->ccAddresses as $ccAddress) { $mail->addCC($ccAddress); } //create message body using $product $mailer = new Mailer(); $mailer->send($mail); } protected $ccAddresses = array(); public function addCcAddress($ccAddress) { $this->ccAddresses[] = $ccAdresss; } foreach($this->ccAddresses as $ccAddress) { $mail->addCC($ccAddress); }

Slide 10

Slide 10 text

Constructor Injection ● Fixed at instantiation ● Guaranteed to have been injected

Slide 11

Slide 11 text

Setter Injection ● Good for optional dependencies ● Good for collections of dependencies

Slide 12

Slide 12 text

protected $toAddress; protected $ccAddresses = array(); protected $mailer; public function __construct($toAddress, MailerInterface $mailer) { $this->toAddress = $toAddress; $this->mailer = $mailer; } public function addCcAddress($ccAddress) { $this->ccAddresses[] = $ccAdresss; } public function notify($product) { $mail = new Mail(); $mail->addTo($this->toAddress); foreach($this->ccAddresses as $ccAddress) { $mail->addCC($ccAddress); } //create message body using $product $this->mailer->send($mail); } protected $mailer; MailerInterface $mailer $this->mailer = $mailer; $this->mailer->send($mail);

Slide 13

Slide 13 text

$settings = Settings::fetch(); switch($settings->get('mailer)) { case 'smtp': $mailer = new SMTPMailer(); break; case 'sendmail': $mailer = new SendmailMailer(); break; case 'test': $mailer = new TestMailer(); break; case 'basic': default: $mailer = new Mailer(); break; } $mailer->send($mail);

Slide 14

Slide 14 text

protected $toAddress; protected $ccAddresses = array(); protected $mailer; public function __construct($toAddress, MailerInterface $mailer) { $this->toAddress = $toAddress; $this->mailer = $mailer; } public function addCcAddress($ccAddress) { $this->ccAddresses[] = $ccAdresss; } public function notify($product) { $mail = new Mail(); $mail->addTo($this->toAddress); foreach($this->ccAddresses as $ccAddress) { $mail->addCC($ccAddress); } //create message body using $product $this->mailer->send($mail); } protected $mailer; MailerInterface $mailer $this->mailer = $mailer; $this->mailer->send($mail);

Slide 15

Slide 15 text

Type Hinting ● Ensures object with correct interface injected ● Hint should be Interface not Implementation ● Helps IDE with auto-completion

Slide 16

Slide 16 text

protected $toAddress; protected $ccAddresses = array(); protected $mailer; public function __construct($toAddress, MailerInterface $mailer) { $this->toAddress = $toAddress; $this->mailer = $mailer; } public function addCcAddress($ccAddress) { $this->ccAddresses[] = $ccAdresss; } public function notify($product) { $mail = new Mail(); $mail->addTo($this->toAddress); foreach($this->ccAddresses as $ccAddress) { $mail->addCC($ccAddress); } //create message body using $product $this->mailer->send($mail); }

Slide 17

Slide 17 text

protected $toAddress; protected $ccAddresses = array(); protected $mailer; public function __construct($toAddress, MailerInterface $mailer) { $this->toAddress = $toAddress; $this->mailer = $mailer; } public function addCcAddress($ccAddress) { $this->ccAddresses[] = $ccAdresss; } public function notify($product) { $mail = MailFactory::get(); $mail->addTo($this->toAddress); foreach($this->ccAddresses as $ccAddress) { $mail->addCC($ccAddress); } //create message body using $product $this->mailer->send($mail); } $mail = MailFactory::get();

Slide 18

Slide 18 text

//-- protected $mailFactory; public function __construct($toAddress, MailerInterface $mailer, MailFactoryInterface $mailFactory ) { $this->toAddress = $toAddress; $this->mailer = $mailer; $this->mailFactory = $mailFactory; } //-- public function notify($product) { $mail = $this->mailFactory->get(); $mail->addTo($this->toAddress); foreach($this->ccAddresses as $ccAddress) { $mail->addCC($ccAddress); } //create message body using $product $this->mailer->send($mail); } protected $mailFactory; MailFactoryInterface $mailFactory $this->mailFactory = $mailFactory; $mail = $this->mailFactory->get();

Slide 19

Slide 19 text

Where does the creation of dependencies go ?

Slide 20

Slide 20 text

class ProductSaver { public function save($product) { $mapper = new DataMapper(); $mapper->save($product); $emailNotifier = new EmailNotifier(); $emailNotifier->notify($product); } }

Slide 21

Slide 21 text

public function save($product) { $mapper = new DataMapper(); $mapper->save($product); $emailNotifier = new EmailNotifier('[email protected]', new Mailer, new MailFactory ); $emailNotifier->setCcAddress('[email protected]'); $emailNotifier->setCcAddress('[email protected]'); $emailNotifier->notify($product); } $emailNotifier = new EmailNotifier('[email protected]', new Mailer, new MailFactory ); $emailNotifier->addCcAddress('[email protected]'); $emailNotifier->addCcAddress('[email protected]');

Slide 22

Slide 22 text

protected $notifiers = array(); public function addNotifier(NotifierInterface $notifier) { $this->notifiers[] = $notifier; } public function save($product) { $mapper = new DataMapper(); $mapper->save($product); foreach($this->notifiers as $notifier) { $notifier->notify($product); } } protected $notifiers = array(); public function addNotifier(NotifierInterface $notifier) { $this->notifiers[] = $notifier; } foreach($this->notifiers as $notifier) { $notifier->notify($product); }

Slide 23

Slide 23 text

protected $notifiers = array(); protected $mapper; public function __construct(DataMapperInterface $mapper) { $this->mapper = $mapper; } //-- public function save($product) { $this->mapper->save($product); foreach($this->notifiers as $notifier) { $notifier->notify($product); } } protected $mapper; $this->mapper->save($product); public function __construct(DataMapperInterface $mapper) { $this->mapper = $mapper; }

Slide 24

Slide 24 text

Problem solved?

Slide 25

Slide 25 text

class ProductController { public function addAction() { //validate the POST variables $productSaver = new ProductSaver(); $productSaver->save($product); } //-- }

Slide 26

Slide 26 text

public function addAction() { //validate the POST variables $emailNotifier = new EmailNotifier('[email protected]', new Mailer, new MailFactory ); $emailNotifier->setCcAddress('[email protected]'); $emailNotifier->setCcAddress('[email protected]'); $mapper = new DataMapper(); $productSaver = new ProductSaver($emailNotifier, $mapper); $productSaver->setNotifier($emailNotifier); $productSaver->save($product); } $emailNotifier = new EmailNotifier('[email protected]', new Mailer, new MailFactory ); $emailNotifier->addCcAddress('[email protected]'); $emailNotifier->addCcAddress('[email protected]'); $mapper = new DataMapper(); $productSaver = new ProductSaver($mapper); $productSaver->addNotifier($emailNotifier);

Slide 27

Slide 27 text

protected $productSaver; public function __construct($productSaver) { $this->productSaver = $productSaver; } public function addAction() { //validate the POST variables $this->productSaver->save($product); }

Slide 28

Slide 28 text

$emailNotifier = new EmailNotifier('[email protected]', new Mailer, new MailFactory ); $emailNotifier->addCcAddress('[email protected]'); $emailNotifier->addCcAddress('[email protected]'); $mapper = new DataMapper(); $productSaver = new ProductSaver($mapper); $productSaver->addNotifier($emailNotifier); $controller = new ProductController($productSaver);

Slide 29

Slide 29 text

Advantages to separate wiring ● Set up config differently for each app ● No config code in classes ● No need to maintain different versions for different apps ● Just inject object with different functionality

Slide 30

Slide 30 text

Disadvantages to manual wiring ● Large unwieldy bootstrap file ● Everything set up whether used or not ● Repetitive code

Slide 31

Slide 31 text

The Symfony2 Service Container to the rescue ● Creates and configures services ● Allows configuration with XML, YAML and PHP ● Only creates used services

Slide 32

Slide 32 text

Slide 33

Slide 33 text

Slide 35

Slide 35 text

services: emailNotifer: class: NameSpace\Of\EmailNotifer arguments: [[email protected], @mailer, @mailFactory] calls: - [ addCcAddress, [ [email protected] ] ] - [ addCcAddress, [ [email protected] ] ] mailer: class: NameSpace\Of\Mailer mailFactory: class: NameSpace\Of\MailFactory

Slide 36

Slide 36 text

$container->setDefinition('emailNotifer', new Definition( 'NameSpace\Of\EmailNotifer', array( '[email protected]', new Reference('mailer'), new Reference('mailFactory') ) ))->addMethodCall('addCcAddress', array( '[email protected]' ))->addMethodCall('addCcAddress', array( '[email protected]' )); $container->setDefinition('mailer', new Definition( 'NameSpace\Of\Mailer' )); $container->setDefinition('mailFactory', new Definition( 'NameSpace\Of\MailFactory' ));

Slide 37

Slide 37 text

How do we actually use the container?

Slide 38

Slide 38 text

$productController = $container->get('productController');

Slide 39

Slide 39 text

class ProductController extends Controller { public function addAction() { //validate the POST variables $productSaver = $this->container->get('productSaver'); $productSaver->save($product); } //-- }

Slide 40

Slide 40 text

A Criticism of Dependency Injection ● It's difficult to navigate code

Slide 41

Slide 41 text

Tools to help ● container:debug command ● JMSDebuggingBundle

Slide 42

Slide 42 text

php app/console container:debug [container] Public services Service Id Class Name doctrine Symfony\Bundle\DoctrineBundle\Registry doctrine.dbal.default_connection Doctrine\DBAL\Connection doctrine.odm.mongodb.cache.array Doctrine\Common\Cache\ArrayCache #--

Slide 43

Slide 43 text

php app/console container:debug assetic.asset_manager [container] Information for service assetic.asset_manager Service Id assetic.asset_manager Class Assetic\Factory\LazyAssetManager Tags - Scope container Public yes

Slide 44

Slide 44 text

JMSDebuggingBundle ● http://github.com/schmittjoh/JMSDebuggingBun dle

Slide 45

Slide 45 text

No content

Slide 46

Slide 46 text

No content

Slide 47

Slide 47 text

No content

Slide 48

Slide 48 text

No content

Slide 49

Slide 49 text

No content

Slide 50

Slide 50 text

Conclusion ● Class level advantages ● Promotes re-usability ● Allows true unit testing ● App level advantages ● Allows easy configuration of application ● Easy to implement new features by dropping in new objects ● No need to maintain multiple code bases

Slide 51

Slide 51 text

More ● Cookbook articles - http://symfony.com/doc/current/cookbook/ ● http://miller.limethinking.co.uk ● @mr_r_miller ● http://joind.in/3704