SlideShare a Scribd company logo
rginjection
               Bastian Hofmann
Introduction to rg\injection
Introduction to rg\injection
Introduction to rg\injection
Introduction to rg\injection
Introduction to rg\injection
Questions? Ask!
Dependency Inversion
http://www.doolwind.com/blog/solid-principles-for-game-developers/
Dependencies
Introduction to rg\injection
Dependency Injection
Constructor
Introduction to rg\injection
Setter
Introduction to rg\injection
Method argument
Introduction to rg\injection
Constructing classes
Introduction to rg\injection
Make it easier with factories
Introduction to rg\injection
Dependency Injection Container
What‘s out there?
Introduction to rg\injection
<?xml version="1.0" ?>
 <container xmlns="http://symfony-project.org/2.0/container">
  <parameters>
    <parameter key="mailer.username">foo</parameter>
    <parameter key="mailer.password">bar</parameter>
    <parameter key="mailer.class">Zend_Mail</parameter>
  </parameters>
  <services>
    <service id="mail.transport"
class="Zend_Mail_Transport_Smtp" shared="false">
      <argument>smtp.gmail.com</argument>
      <argument type="collection">
        <argument key="auth">login</argument>
        <argument key="username">%mailer.username%</argument>
        <argument key="password">%mailer.password%</argument>
        <argument key="ssl">ssl</argument>
        <argument key="port">465</argument>
      </argument>
    </service>
    <service id="mailer" class="%mailer.class%">
      <call method="setDefaultTransport">
        <argument type="service" id="mail.transport" />
      </call>
    </service>
  </services>
</container>
rginjection
    inspired by google-guice
    http://code.google.com/p/google-guice/
Creating an instance of a class
Introduction to rg\injection
Introduction to rg\injection
Automatic creation of dependencies through Typehints
Introduction to rg\injection
@inject
Introduction to rg\injection
What classes can be injected?
No constructor
Introduction to rg\injection
Constructor without arguments
Introduction to rg\injection
Constructor with arguments that have default values
Introduction to rg\injection
Injectable constructor
Introduction to rg\injection
Introduction to rg\injection
By the way: Property Injection
Introduction to rg\injection
Singletons with public static getInstance method following
                      the same rules
Introduction to rg\injection
STOP: Don‘t use Singeltons
Singletons are evil!
Let the DIC take care of instances
@singleton
Introduction to rg\injection
By the way...
Introduction to rg\injection
Passing additional, fixed values to instance
Introduction to rg\injection
What about testing
Only test the class, not the dependencies
Introduction to rg\injection
Mocking
Introduction to rg\injection
Introduction to rg\injection
Working with Interfaces
Why interfaces?
Introduction to rg\injection
But we can not create an instance of an Interface!
@implementedBy
Introduction to rg\injection
Mocking an interface
Introduction to rg\injection
Named @implementedBy
Introduction to rg\injection
Introduction to rg\injection
What‘s that for?
Providers
@providedBy
Introduction to rg\injection
Introduction to rg\injection
Method invocation at DIC
Introduction to rg\injection
Same rules
Introduction to rg\injection
Introduction to rg\injection
And if you don‘t like annotations...
... or want to integrate 3rd party code
Introduction to rg\injection
Introduction to rg\injection
Integrate it into your framework
Great! So, how does it work internally?
Development: Reflection
Production: Generated Factory Classes
DEMO
Available? very soon
h"p://twi"er.com/Bas2anHofmann
h"ps://profiles.google.com/bashofmann
h"p://lanyrd.com/people/Bas2anHofmann/
h"p://slideshare.net/bashofmann

mail@bas2anhofmann.de

More Related Content

Introduction to rg\injection