Presentation is loading. Please wait.

Presentation is loading. Please wait.

DI (Dependency Injection) / IOC (Inversion Of Control)

Similar presentations


Presentation on theme: "DI (Dependency Injection) / IOC (Inversion Of Control)"— Presentation transcript:

1 DI (Dependency Injection) / IOC (Inversion Of Control)
PHP+SQL 7. DI (Dependency Injection) / IOC (Inversion Of Control) OE NIK, 2013

2 It is hard to write a good code!
Keywords: decoupling, loose coupling! Ránézésre olvasható nevekkel: + ránézésre olvasható teszt is legyen Az egymástól való függetlenítés miatt akár kódduplikáció is simán megengedett

3 This also helps us in writing good tests!
The tests must be fast Slow tests are impossible to run over and over again  test will not be executed, bugs will be found later The tests must be independent Order, timing, etc. must not affect the results Naming convention must be easy-to-read A good test list is basically a requirement-list We must not cover every possible inputs Examples are good, finding the corner cases are important! Only test a single feature of a single class Always independent from the live data (database/settings) We can substitute the dependencies too: Dependency Injection + fake dependencies, mocking... Ránézésre olvasható nevekkel: + ránézésre olvasható teszt is legyen Az egymástól való függetlenítés miatt akár kódduplikáció is simán megengedett

4 Dependency Injection We provide the dependencies of the classes externally – via interfaces, which allows us To exchange the dependency later at any time to another class – while keeping the interface To perform “truly” independent individual unit-tests When developing, we can use any instance of the interface The instance can be automatically created: DI container Dependencies of the dependencies: IoC container When testing, we replace the dependency with a known and simple object solely created for the tests The class will be independent from the dependency Various ways: fakes, mocks, dummies, stubs …

5 Constructor Injection
interface IMyDependency { string DoSomething(); } class MyClass private IMyDependency dependency; public MyClass(IMyDependency dependency) this.dependency = dependency; // in methods: dependency.DoSomething() // usage IMyDependency myDependency; // + instance creation somehow... MyClass myClass = new MyClass(myDependency); OE-NIK HP

6 Dependency Injection OE NIK, 2013

7 IoC/Service container
OE NIK, 2013

8 In symfony In a controller, $this->container is an IoC container
// controller shortcut $service = $this->get('xxx'); // real call $service = $this->container->get('xxx'); $service = $instanceOfContainerInterface->get('xxx'); Services can be defined in services.yml to be used later services: app.carfactory: class: AppBundle\Service\CarServiceFactory arguments: app.carservice: class: AppBundle\Service\ICarService factory: getService] OE NIK, 2013

9 In symfony Download & uncompress php_code_06_ormEntities.zip
php bin/console debug:container app (also: debug:route, debug:twig) TO READ: dependency-injection-vs-service-locator-best-practice dependency-injection-with-symfony-2 OE NIK, 2013

10 OE NIK, 2013

11 OE NIK, 2013


Download ppt "DI (Dependency Injection) / IOC (Inversion Of Control)"

Similar presentations


Ads by Google