import
as an anti-pattern
Demystifying Dependency
Injection in Python
Yeray Díaz PyCon UK
September 2019
@yera_ee
Slide 2
Slide 2 text
! I’m Yeray
• Freelance Software Engineer based in
London
• Co-organizer of London Python
Meetup
• Moderator and contributor for PyPI
Slide 3
Slide 3 text
Dependency
Injection
Slide 4
Slide 4 text
Dependency
Injection
Worst name ever?
Slide 5
Slide 5 text
Dependency… Injection?
Daniel Frank
https://xkcd.com/327/
Slide 6
Slide 6 text
What do programmers think?
Daniel Frank
https://twitter.com/yera_ee/status/1072540888264597506
Slide 7
Slide 7 text
What do programmers think?
Daniel Frank
https://twitter.com/yera_ee/status/1072540888264597506
Slide 8
Slide 8 text
How does DI
make you feel?
Slide 9
Slide 9 text
No content
Slide 10
Slide 10 text
No content
Slide 11
Slide 11 text
No content
Slide 12
Slide 12 text
No content
Slide 13
Slide 13 text
No content
Slide 14
Slide 14 text
No content
Slide 15
Slide 15 text
No content
Slide 16
Slide 16 text
No content
Slide 17
Slide 17 text
No content
Slide 18
Slide 18 text
No content
Slide 19
Slide 19 text
No content
Slide 20
Slide 20 text
No content
Slide 21
Slide 21 text
No content
Slide 22
Slide 22 text
No content
Slide 23
Slide 23 text
No content
Slide 24
Slide 24 text
No content
Slide 25
Slide 25 text
No content
Slide 26
Slide 26 text
Coupling
Slide 27
Slide 27 text
Where did it all go wrong?
Slide 28
Slide 28 text
Where did it all go wrong?
Slide 29
Slide 29 text
Where did it all go wrong?
Slide 30
Slide 30 text
import is an
anti-pattern
Slide 31
Slide 31 text
No content
Slide 32
Slide 32 text
No content
Slide 33
Slide 33 text
“Depend on
abstractions,
not concretions”
Slide 34
Slide 34 text
What is an abstraction?
•A formal interface definition
•A series of objects that implement
that interface
•Encapsulating the lower level details
Slide 35
Slide 35 text
In our case
Slide 36
Slide 36 text
In our case
Slide 37
Slide 37 text
Or even better…
Slide 38
Slide 38 text
And now…
Slide 39
Slide 39 text
And now…
Slide 40
Slide 40 text
And now…
Slide 41
Slide 41 text
And now…
Slide 42
Slide 42 text
And now…
Slide 43
Slide 43 text
And now…
Dependency
Injection!
Slide 44
Slide 44 text
Going concrete
Slide 45
Slide 45 text
Going concrete
Slide 46
Slide 46 text
FileProcessor
Google Storage
What just happened?
Slide 47
Slide 47 text
FileProcessor
GCSFileRepo
FileRepoABC
Slide 48
Slide 48 text
FileProcessor
GCSFileRepo
FileRepoABC
Slide 49
Slide 49 text
FileProcessor
GCSFileRepo
FileRepoABC
Inversion of
Control
Slide 50
Slide 50 text
What just happened?
•FileProcessor now has one
responsibility: compute the hash of
file contents
•It no longer instantiates the repo, this
responsibility is moved to the caller
Slide 51
Slide 51 text
Where do dependencies
come from?
• One place where all instances
of all dependencies are created
• The Composition Root
• Usually at the entry point of
our application
Slide 52
Slide 52 text
Composition root
Slide 53
Slide 53 text
Composition root
Slide 54
Slide 54 text
Composition root
Slide 55
Slide 55 text
Composition root
Slide 56
Slide 56 text
Benefits of DI
• Interception
• Lifetime
• Composition
Slide 57
Slide 57 text
Interception
Slide 58
Slide 58 text
Interception
Slide 59
Slide 59 text
Interception
Liskov
Substitution
Principle
Slide 60
Slide 60 text
But of course…
Slide 61
Slide 61 text
But of course…
Boundary
test
Slide 62
Slide 62 text
Boundary tests
•Isolated from changes in the business
logic
•We test that it implements the interface
correctly
•Will not change at all if the interface is
stable
•Possibility for integration tests
Pure DI
•DI Frameworks aim to simplify
the composition
•DI containers vs Pure DI
•DI containers are NOT required
Slide 75
Slide 75 text
Where does DI shine?
Slide 76
Slide 76 text
Where does DI shine?
•Where the abstraction is clear
Slide 77
Slide 77 text
Where does DI shine?
•Where the abstraction is clear
•Runtime dependencies and
boundaries
Slide 78
Slide 78 text
Where does DI shine?
•Where the abstraction is clear
•Runtime dependencies and
boundaries
•Business logic abstractions may be
harder to define and costly to
backtrack
Slide 79
Slide 79 text
Conclusions
•Every import of an external library is
creating coupling
•Consider the responsibility that’s being
fulfilled
•Abstract only if it’s obvious
•You do not need to go all-in with
frameworks, DI can be applied gradually
Slide 80
Slide 80 text
Further reading
https://www.manning.com/books/dependency-injection-principles-practices-patterns
Slide 81
Slide 81 text
Further reading
https://www.sandimetz.com/99bottles
Slide 82
Slide 82 text
Further reading
https://www.destroyallsoftware.com/talks/boundaries