dependency injection containers

Download Dependency Injection Containers

Post on 07-Jul-2015

1.025 views

Category:

Technology

5 download

Embed Size (px)

TRANSCRIPT

  • 1. Dependency Injection ContainersangaDependency Injection ContainersVilniusPHP Susitikimas #3 Povilas Balzaraviius 2013 m. vasario 7 d.

2. Dependency Injection ContainersangaKas a toks? Povilas Balzaraviius @Pawka github.com/pawka linkedin.com/in/pawka pawka.linija.net 3. Dependency Injection ContainersDependency Injection Kas yra Dependency Injection?Kas yra Dependency Injection? 4. Dependency Injection ContainersDependency InjectionKas yra Dependency Injection?Kuo blogas is kodas?1 class FeedGenerator {23 protected $feed;45 public function __construct() {6 // Create feed every 6 hours.7 $this->feed = new SomeFeed(0 */6 * * *);8 }9 } 10 //.. 11 $generator = new FeedGenerator; 5. Dependency Injection ContainersDependency InjectionKas yra Dependency Injection?Kuo blogas is kodas?1 class FeedGenerator {23protected $feed;45public function __construct() {6// Create feed every 6 hours.7$this->feed = new SomeFeed(0 */6 * * *);8}9 } 10 //.. 11 $generator = new FeedGenerator; 1 Sudtinga parayti test. 2 Gali tekti keisti kod modikuojant Feed klas. 3 Teks modikuoti FeedGenerator klas, norint pakeisti SomeFeed vykdymo interval. 6. Dependency Injection ContainersDependency InjectionKas yra Dependency Injection?Ar dabar geriau?1 class FeedGenerator {23 protected $feed;45 public function __construct($interval) {6 $this->feed = new SomeFeed($interval);7 }8 }9 //.. 10 $generator = new FeedGenerator(0 */6 * * *); 7. Dependency Injection ContainersDependency InjectionKas yra Dependency Injection?Ar dabar geriau?1 class FeedGenerator {23protected $feed;45public function __construct($interval) {6$this->feed = new SomeFeed($interval);7}8 }9 //.. 10 $generator = new FeedGenerator(0 */6 * * *); 1 Sudtinga parayti test. 2 Gali tekti keisti kod modikuojant Feed klas. 3 SomeFeed klas vis dar priklausoma nuo FeedGenerator. 8. Dependency Injection ContainersDependency Injection Kas yra Dependency Injection?K daryti? Ikelti vidini objekt krimu klass rib! 9. Dependency Injection ContainersDependency InjectionKas yra Dependency Injection?Kodl is kodas geresnis?1 namespace Feed;23 class FeedGenerator {45protected $feed;67public function __construct(FeedInterface $feed) {8$this->feed = $feed;9} 10 } 11 //.. 12 $feed = new SomeFeed(0 */6 * * *); 13 $generator = new FeedGenerator($feed); 1 Patogu rayti testus (vietoj Feed galima paduoti mock objekt). 2 $feed objektas nepriklauso nuo FeedGenerator klass. 3 Modikuojant FeedInterface klases, nereiks keisti FeedGenerator kodo. 10. Dependency Injection ContainersDependency Injection Kas yra Dependency Injection?Sveiki, a DItai Jums Dependency Injection! 11. Dependency Injection ContainersDependency InjectionKas yra Dependency Injection?Objekt krimas DI naudojimui konteineris nereikalingas.1 $config = new DoctrineDBALConfiguration();2 $connectionParams = array(3 dbname => mydb,4 user => user,5 password => secret,6 host => localhost,7 driver => pdo_mysql,8 );9 $conn = DoctrineDBALDriverManager::getConnection( 10 $connectionParams, $config); 11 $productService = new Product($conn); 12 $interval = "0 */6 * * *"; 13 $feed = new FeedSomeFeed($interval); 14 $feed->setProductService($productService); 15 $feedGenerator = new FeedFeedGenerator($feed); 16 //Maybe add objects to some registry. 17 //.. 12. Dependency Injection ContainersDependency Injection Kas yra Dependency Injection?Kas yra DI? Projektavimo ablonas (design pattern). Aktualus tik objektiniame programavime. Atskiria komponentus (decoupling). Standartizuoja ir suteikia galimyb centralizuoti objekt krim Js sistemoje. 13. Dependency Injection ContainersDependency InjectionKas yra Dependency Injection?DI bdai1 $feed = new Feed("0 */6 * * *");23 //Konstruktorius4 $generator = new FeedGenerator($feed);56 //Metodas (setter)7 $generator = new FeedGenerator;8 $generator->setFeed($feed);9 10 //Atributas 11 $generator = new FeedGenerator; 12 $generator->feed = $feed; 14. Dependency Injection ContainersDIC bibliotekosKam kurti savo jei yra..DIC bibliotekos 15. Dependency Injection ContainersDIC bibliotekosReikalavimai DI konteineriuiTuri veikti greitai.Privalo dirbti su bet kokiu PHP objektu.Objektai neturi inoti apie DI konteiner.Nekurti objekto jei jis jau sukurtas.1 1 Kai to reikia. 16. Dependency Injection ContainersDIC bibliotekos PimplePimple 17. Dependency Injection ContainersDIC bibliotekos PimplePimple A simple Dependency Injection Container for PHP 5.3. Autorius: Fabien Potencier pimple.sensiolabs.org Paprastas. Greitas. Integravimas - kelios eiluts. 18. Dependency Injection ContainersDIC bibliotekosPimpleDarbas su objektais1 $container = new Pimple();2 // ..34 $container[feed.somefeed.interval] = 0 */6 * * *;5 $container[feed.somefeed.class] = FeedSomeFeed;67 $container[feed.somefeed] = function($c) {8 return new $c[feed.somefeed.class](9 $c[feed.somefeed.interval] 10 ); 11 }; 12 13 //Lazy loading 14 $container[feed.generator.class] = FeedFeedGenerator; 15 $container[feed.generator] = $container->share(function($c) { 16 return new $c[feed.generator.class]($c[feed.somefeed]); 17 }); 18 19 // .. 20 $manager = $container[feed.generator]; 19. Dependency Injection ContainersDIC bibliotekosPimpleIskaidymas modulius1 class FeedContainer extends Pimple {23public function __construct() {4$this[feed.somefeed.interval] = 0 */6 * * *;5$this[feed.somefeed.class] = FeedSomeFeed;67 $this[feed.somefeed] = function($c) {8 return new $c[feed.somefeed.class](9 $c[feed.somefeed.interval] 10 ); 11 }; 12 13 $this[feed.generator.class] = FeedFeedGenerator; 14 $this[feed.generator] = $this->share(function($c) { 15 return new $c[feed.generator.class]($c[feed.somefeed]); 16 }); 17} 18 } 20. Dependency Injection ContainersDIC bibliotekosPimpleIskaidymas modulius1 class GlobalContainer extends Pimple {23public function __construct() {4$this[feeds] = $this->share(function($c) {5return new FeedContainer;6};7}8 }9 10 //.. 11 $container = new GlobalContainer; 12 //Change value 13 $container[feeds][feed.somefeed.interval] = 0 */12 * * *; 14 $generator = $container[feeds][feed.generator]; 21. Dependency Injection ContainersDIC bibliotekos PimpleSparta Konteinerio krimas./run-tests.shPimple265K579K0.00086sDependencyInjection267K931K0.006124sDependencyInjection (dumped)688K738K0.000495s altinis: https://gist.github.com/igorw/3833123 22. Dependency Injection ContainersDIC bibliotekos Symfony Dependency Injection ComponentSymfony Dependency InjectionComponent 23. Dependency Injection ContainersDIC bibliotekos Symfony Dependency Injection ComponentSymfony DIC Symfony komponentas. http://symfony.com/components Daug galimybi. Greitas (teisingai naudojant). Pagal nutyljim naudojamas Symfony2 karkase. 24. Dependency Injection ContainersDIC bibliotekos Symfony Dependency Injection ComponentPalaikomi formatai Prikalusomybes galima aprayti iais formatais:PHPXMLYAMLINI (Palaiko tik parametr apraymus). 25. Dependency Injection ContainersDIC bibliotekosSymfony Dependency Injection ComponentNaudojimas: PHP1 use SymfonyComponentDependencyInjectionContainerBuilder;2 use SymfonyComponentDependencyInjectionReference;34 $container = new ContainerBuilder();56 $container->setParameter(feed.somefeed.interval, 0 */6 * * *);7 $container8 ->register(feed.somefeed, SomeFeed)9 ->addArgument(%feed.somefeed.interval%); 10 11 $container 12 ->register(feed.generator, FeedGenerator) 13 ->addArgument(new Reference(feed.somefeed)); 26. Dependency Injection ContainersDIC bibliotekosSymfony Dependency Injection ComponentNaudojimas: XML1 2 0 */6 * * *3 45 6 7 %feed.somefeed.interval%8 9 10 11 12 13 27. Dependency Injection ContainersDIC bibliotekosSymfony Dependency Injection ComponentNaudojimas: YAML1 parameters:2 # ...3 feed.somefeed.interval: 0 */6 * * *45 services:6 feed.somefeed:7 class:SomeFeed8 arguments: [ % feed.somefeed.interval%]9 feed.generator: 10 class:FeedGenerator 11 arguments: [ @ feed.somefeed] 28. Dependency Injection ContainersDIC bibliotekosSymfony Dependency Injection ComponentDI perdengimas Palaiko YAML ir XML formatai. Leidia perdengti anksiau apraytas DI priklausomybes (pvz. dirbant su Symfony2). Patogu naudoti skirtingas kongracijas: test, dev, live.1 use SymfonyComponentDependencyInjectionContainerBuilder;2 use SymfonyComponentConfigFileLocator;3 use SymfonyComponentDependencyInjectionLoaderXmlFileLoader;45 $container = new ContainerBuilder();6 $loader = new XmlFileLoader($container, new FileLocator(__DIR__));7 $loader->load(services.xml); 29. Dependency Injection ContainersDIC bibliotekos Symfony Dependency Injection ComponentKompiliavimasKonteineris gali bti kompiliuojamas.1 $container->compile();Aptinkamos klaidos: Neegzistuojanios priklausomybs. Ciklins priklausomybs.Atliekama optimizacija.Sukompiliuotas rezultatas gali bti saugomas disk irnaudojamas kaip cacheas.2 P.S. Symfony2 tuo rpinasi pagal nutyljim :-) 2 Pamenat spartos palyginimo rezultatus? 30. Dependency Injection ContainersDIC bibliotekos Symfony Dependency Injection ComponentSukompiliuoto rezultato saugojimas disk Naudojamos Dumper klass. Galima konvertuoti i vieno formato kit. Palaikomi formatai: PHP XML YAML GraphWiz (www.graphwiz.org) 31. Dependency Injection ContainersDIC bibliotekosSymfony Dependency Injection ComponentSukompiliuoto rezultato saugojimas disk1 use SymfonyComponentDependencyInjectionContainerBuilder;2 use SymfonyComponentDependencyInjectionDumperPhpDumper;34 $file = __DIR__ ./cache/container.php;56 if (file_exists($file)) {7 require_once $file;8 $container = new ProjectServiceContainer();9 } else { 10 $container = new ContainerBuilder(); 11 // ... 12 $container->compile(); 13 14 $dumper = new PhpDumper($container); 15 file_put_contents($file, $dumper->dump()); 16 } ProjectServiceContainer - pavadinimas pagal nutyljim. 32. Dependency Injection ContainersDIC bibliotekos Symfony Dependency Injection ComponentTagged servisai Tag - atributas, ymintis panaius objektus. Lei

Recommended

View more >