Top Banner
Dependency Injection and Symfony
40

Dependency injection with Symfony 2

May 25, 2015

Download

Technology

cammanderson

A presentation on getting to understand dependency injection as well as the service container in Symfony2
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Dependency injection with Symfony 2

Dependency Injection and Symfony

Page 2: Dependency injection with Symfony 2

Introduction

Cameron MandersonDeveloper, Director of Flint, Melbourne Symfony2 Organiser

[email protected]: @cammandersonwww.linkedin.com/in/cameronmanderson

Page 3: Dependency injection with Symfony 2

Topic Highlight

• More in-depth look at the background of “Dependency Injection”

• Aiming to uncover in Symfony– Modularity in our implementation– Loose decoupling of dependencies– Managing scope and access to dependencies

Page 4: Dependency injection with Symfony 2

Background

Page 5: Dependency injection with Symfony 2

Some terminology

• Dependency– Something that is required by another object to

fulfill its function• Dependent– A client object that needs a dependency in order

to perform its function

Page 6: Dependency injection with Symfony 2

Shown easiest like this..

Dependent Dependency

Emailer Spell Checker

Like…

Page 7: Dependency injection with Symfony 2

Example in code

Page 8: Dependency injection with Symfony 2

This seems fine.. Right?

Page 9: Dependency injection with Symfony 2

What about English or French?

Page 10: Dependency injection with Symfony 2

Can we test it was called? – no…

Page 11: Dependency injection with Symfony 2

We need an approach to handling these dependencies.

We need looser coupling.

Page 12: Dependency injection with Symfony 2

Dependency Injection

So what is Dependency Injection?

Page 13: Dependency injection with Symfony 2

Dependency Injection

Dependency Injection“A design pattern whose purpose is to improve the testability of, and simplify deployment of components”

Essentially something that takes care of creating and linking our objects

Involves 3 parts• Our 2 objects; dependency and dependent• And.. An Injector to instantiate objects at runtime

Page 14: Dependency injection with Symfony 2

Example

Injector configures our objects at runtime, figuring out the order of creation

Dependent Dependency

InjectorInjector reads the dependencies, and creates and stiches them together

Page 15: Dependency injection with Symfony 2

So.. Why dependency injection?

• Why do we need better management?– Applications are full of objects and dependencies• We want automation of construction• We want easy management of objects• We don’t want hundreds of Factories

– Hard to manage those dependencies• Applications become tightly coupled to one-another• Difficult to swap out implementations• Manage configuration of those dependencies

Page 16: Dependency injection with Symfony 2

Pre-dependency injection

• How do we currently do it?– Hand-written construction– Using Factory patterns

Page 17: Dependency injection with Symfony 2

Hand-construction

Page 18: Dependency injection with Symfony 2

Factory

Page 19: Dependency injection with Symfony 2

Enter Dependency Injection

• Offset the instantiation of those objects to something else, as well as the dependencies– Some behind the scenes magic– Read a definition of how these dependencies

should be handled– Create the objects and associate their

dependencies• Scope (talk about later)

Page 20: Dependency injection with Symfony 2

Injector/idioms

• Simple logic:– Can it create the object yet? If there is a

dependency, go create it.. Loop etc.• Essentially… a role to..– Creating the “new” object when needed– Managing the dependencies • Constructor Injection• Setter Injection

Page 21: Dependency injection with Symfony 2

Injector Configuration

Page 22: Dependency injection with Symfony 2

Plain PHP files..

Page 23: Dependency injection with Symfony 2

Constructor Injection

• Constructor injection has dependencies in the constructor– Need to create the dependencies for the

dependent first– Inject them through the constructor

Page 24: Dependency injection with Symfony 2

Setter Injection

• Create the object first and inject the dependents through methods (setters)

Page 25: Dependency injection with Symfony 2

Some stuff to be careful of…

• Circular dependencies– Oops. Be careful. (There are ways to handle.. Not

yet in Symfony)

C B

A

Page 26: Dependency injection with Symfony 2

So how does this help?

• We are all about building modular applications– Decoupling dependencies– Making clear what the intent of the module is– Separation of concerns– Improve testability

Page 27: Dependency injection with Symfony 2

Decoupling

• Reduce the specific hard-value reliance– Facilitate swapping out new implementations – Reduce that complexity– Hopefully reduce the risk..

Page 28: Dependency injection with Symfony 2

Make clear what it is

• We want clear intent to our application– Make it easy to use and modify– “Program to contract”• Implement interfaces that clearly defines the intent of

our code

Page 29: Dependency injection with Symfony 2

Separation of concerns

• We want to better encourage design to separate the concerns in our application– Discreet features

• Application Logic vs Infrastructure Logic

Application

Infrastructure

Page 30: Dependency injection with Symfony 2

Testability

• Keep it clean– Avoiding static calls or singletons– Out of container testing easy– Easy to test concerns individually and manually

define dependencies and mocks

Page 31: Dependency injection with Symfony 2

We are lazy

• It is much easier utilising a dependency injector to handle creating objects and handle those dependencies than to worry about implementing patterns and configuration yourself.

Page 32: Dependency injection with Symfony 2

Basics… check!

• That’s the theory– Dependencies exist in our application– Dependency injection assists creating and

managing those objects– Makes our life easier

• Let’s talk about Symfony and Services..

Page 33: Dependency injection with Symfony 2

Symfony Service Architecture

• Symfony is an Event Driven application• Also provides a dependency injector and

service locator

Page 34: Dependency injection with Symfony 2

The Service

Service– An object that performs a well defined function

when called upon– Separated from other parts of the application– Implements an action• e.g. Template Renderer, Mail, etc

Page 35: Dependency injection with Symfony 2

Configuring the Injector

Page 36: Dependency injection with Symfony 2

Service Locator (Service Container)

• Assists by providing a way to access services by “key”

Page 37: Dependency injection with Symfony 2

Finding Keys

• Might be a problem?• Symfony makes it easy, use the CLI

Page 38: Dependency injection with Symfony 2

Further stuff to read…

• Topics– Aliasing– Public and Private– Specifying parameters in your config.yml

• http://symfony.com/doc/current/book/service_container.html

Page 39: Dependency injection with Symfony 2

Wrapping it up

• My experience with it has been that:– You are encouraged to write services that others

can use and extend– Your applications are much easier to test– Scope is much easier to manage– It encourages others to implement their own

domain logic when needed, thanking you how easy it is to implement their own part. ;-)

Page 40: Dependency injection with Symfony 2

Conclusion

• Covered the concepts of dependency injection– The problem– Using dependency injection to remove (code)

• Showed how it applies in Symfony– Better decoupling, modularity and reuse