Upgrade to PRO for Only $50/Year—Limited-Time Offer! 🔥

Dependency injection v Nette 2.1 prakticky

Dependency injection v Nette 2.1 prakticky

Filip Procházka

January 27, 2014
Tweet

More Decks by Filip Procházka

Other Decks in Technology

Transcript

  1. Co si povíme? - rychle prolítneme teorii DI - jak

    to funguje v Nette - jak napsat použitelné rozšíření
  2. Konstruktor injection class Cart extends Nette\Object { private $entityManager; public

    function __construct(EntityManager $em) { $this->entityManager = $em; } http://wbab.suffolk.lib.ny.us/wp-content/uploads/2013/12/medals.jpg
  3. Setter injection class Cart extends Nette\Object { private $entityManager; public

    function setEntityManager(EntityManager $em) { $this->entityManager = $em; } http://wbab.suffolk.lib.ny.us/wp-content/uploads/2013/12/medals.jpg
  4. Property injection class Cart extends Nette\Object { /** @var EntityManager

    */ public $entityManager; http://wbab.suffolk.lib.ny.us/wp-content/uploads/2013/12/medals.jpg
  5. Magic injection class Cart extends Nette\Object { /** @var EntityManager

    */ private $entityManager; http://www.wyatt-lorenz.com/images/biohazard_warnings.jpg
  6. Presentery - konstruktor (myslím že ne Time) - setter inject*()

    (přihořívá) - property @inject (vítěz!) /** @var Damejidlo\Cart @inject */ public $cart;
  7. Ruční továrničky class OrderFormFactory { private $em; function __construct(EntityManager $em)

    { … } /** @return OrderForm */ public function create() { return new OrderForm($this->em); }
  8. Generované továrničky /** @var IOrderFormFactory @inject */ public $orderFormFactory; protected

    function createComponentOrderForm() { $form = $this->orderFormFactory->create(); $form->onSuccess[] = ...; return $form }
  9. Rozšíření class OrmExtension extends CompilerExtension { # vytvori služby public

    function loadConfiguration() { } # upravuje své a jiné služby public function beforeCompile() { } # hackuje DI container public function afterCompile() { }
  10. Dobré rozšíření - Composer - MUST HAVE - Jednoduchá registrace

    - Convention over Configuration - validujte