SlideShare ist ein Scribd-Unternehmen logo
1 von 35
What is wrong with Dependency
Injection?
Igor Kochetov (@ k04a)
Unity Technologies
@k04a
The DI book
2
Authored by:
Mark “ploeh”
Seemann
Steven “.NET
Junkie” van
Deursen
Who am I?
Dev (C#, Python) in Opsy (Docker, k8s, GCP) world
Attendee: NDC (Oslo), New Crafts (Paris)
Speaker: BuildStuff (Vilnius), XP Days & .Net Fest (Kyiv)
Religious about engineering practices and team culture,
software design and good code
Toolsmith at Unity Technologies, based in Odessa
Twitter: @k04a
Linkedin: Igor Kochetov
@k04a
Why we even talk about DI in 2019?
There are conflicting opinions on the
subject of DI
Is it even still relevant?
4
@k04a
What is DI?
5
@k04a
Nice one, but there are still questions...
Who’s “we” who make sure you have
what you need?
How do we state “the need” in the code?
Why one would use it? What are the
benefits, actually?
6
Known benefits
Dependency Injection
❏ Reducing dependencies of
components (and implementation
details of those dependencies)
❏ Reusable code / swapping of
components
❏ More testable code
❏ More readable code (by defining
dependencies clearly)
Loose coupling
Isolated components are easier to:
❏ Modify, reuse or compose
differently
❏ Test
❏ Reason about
7
@k04a
It is about Dependencies...
Identifying dependencies in our
code (and types of dependencies)
Managing dependencies
8
@k04a
Programming to interfaces and D in SOLID
High-level modules should not depend
on low-level modules. Both should
depend on abstractions.
Abstractions should not depend on
details. Details should depend on
abstractions.
9
@k04a
Also related: who owns the abstractions?
Model abstractions around
domain-specific scenarios instead
of low-level implementation details
“Adapt” external dependencies to
match your domain concepts
Hexagonal architecture or
“Ports & Adapters”:
10
@k04a
Programming to interfaces and D in SOLID
ServiceX is still tightly
coupled to ServiceY!
11
How do we state “the need” in the code?
12
@k04a
Resolve dependencies using Service Locator
At first, looks like very good idea,
which makes it even more
dangerous.
But now we introduce new
dependency (on the container)
We also hide actual dependencies
How would you write tests for it?
13
@k04a
Constructor Injection is the way to go
❏ Explicitly lists
dependencies
❏ Gives clues about what
class might do
❏ Also an indicator of
possible SRP violations
14
@k04a
Wiring decoupled codebases
15
Who’s “we” who make sure you have
what you need?
Or “Mommy and Daddy providing a drink”
16
@k04a
Many platforms provide out-of-the box DI capabilities
.NET MVC / .NET Core MVC
Spring in Java
Angular JS
Kind of making it a de-facto standard
17
Myth: DI is a software library or a
framework
Or at least requires one to be done properly
@k04a 18
@k04a
IoC Container or DI Container
Container - software library that
‘resolves dependencies’ and manages
their ‘lifetime’
❏ Simplifies wiring and reduces
maintenance
❏ Simplifies testing
❏ Allows for dynamic configuration
❏ Supports AOP
But… Is it?
19
@k04a
Manually registering components in the container
20
@k04a
Composition Root and Pure DI
Creating all the stuff
‘normally’ in
Composition Root
(console app example)
21
@k04a
Is it OK to have all the components in the app root?
It is good idea to keep individual
components separated
Until we have to actually assemble
working application
22
@k04a
Explicit is better than implicit (Zen of Python)
Cost of writing vs cost of reading (or imagine
bringing a new member of the team into the
codebase)
Let IDE and code analysis tools be your
friends
It is actually IMPORTANT to know, what
your app does (well-structured Composition
Root serves as documentation providing
bird-eye view on high-level features and use-
cases)
23
@k04a
Composing within framework (.Net Core MVC)
As easy as registering a
custom IControllerActivator
24
@k04a
Composing within framework (.Net Core MVC)
… and implementing it
25
@k04a
Use-case for Aspect-Oriented-Programming
Applying cross-cutting concerns (also
known as non-functional requirements)
like:
❏ Logging
❏ Caching
❏ Security
26
@k04a
The power of Decorators (or AOP done right)
Wrap logging around any
actual implementation
using Decorator design
pattern
27
@k04a
Injecting decorated dependency
Wrap any number of
cross-cutting concerns
around actual
implementation
28
@k04a
Applying SOLID to extreme pushes you toward FP
A lot of single-method
interfaces and small focused
classes (implementations)
Which is good, until you have
to write Decorators for every of
them
29
@k04a
CQS and Parameter Object
Single generic
abstraction
Implementation
consists of two types:
❏ Command
❏ CommandHandler
30
@k04a
Decorator over generic ICommandHandler
Now every ‘aspect’
becomes single
Decorator over
generic
ICommandHandler
<TCommand>
interface
31
@k04a
Decorator over generic ICommandHandler
Write once, use
for all kinds of
Commands
32
@k04a
Conclusions & takeaways
Container is a dependency itself
It also might be a sign your codebase
grew too big (read “monolith”)
DI ≠ Container
DI is useful as a design pattern and
allows greater flexibility and more
manageable codebase (easier to test,
modify and reason about)
33
@k04a
The big idea (or what those have in common?)
❏ Dependency Injection (DI)
❏ Continuous Integration (CI) / Continuous Deployment (CD)
❏ Scrum, Lean or any Agile methodology / framework of your
choice
❏ DevOps
Answer: Those were meant to be PRACTICES (not Tools)
34
Q & A
35

Weitere ähnliche Inhalte

Was ist angesagt?

Was ist angesagt? (20)

Applying EF Code First at Your Job
Applying EF Code First at Your JobApplying EF Code First at Your Job
Applying EF Code First at Your Job
 
Empower every Azure Function to achieve more!!
Empower every Azure Function to achieve more!!Empower every Azure Function to achieve more!!
Empower every Azure Function to achieve more!!
 
The Elephant in the Kubernetes Room - Team Interactions at Scale @ KubeCon No...
The Elephant in the Kubernetes Room - Team Interactions at Scale @ KubeCon No...The Elephant in the Kubernetes Room - Team Interactions at Scale @ KubeCon No...
The Elephant in the Kubernetes Room - Team Interactions at Scale @ KubeCon No...
 
Lesson 1 Understanding Dot Net Framework
Lesson 1   Understanding Dot Net FrameworkLesson 1   Understanding Dot Net Framework
Lesson 1 Understanding Dot Net Framework
 
Performance testing DataHelix | Scott Logic
Performance testing DataHelix | Scott LogicPerformance testing DataHelix | Scott Logic
Performance testing DataHelix | Scott Logic
 
Module 1: Introduction to .NET Framework 3.5 (Slides)
Module 1: Introduction to .NET Framework 3.5 (Slides)Module 1: Introduction to .NET Framework 3.5 (Slides)
Module 1: Introduction to .NET Framework 3.5 (Slides)
 
Overview of .Net Framework
Overview of .Net FrameworkOverview of .Net Framework
Overview of .Net Framework
 
An isas presentation on .net framework 2.0 by vikash chandra das
An isas presentation on .net framework 2.0 by vikash chandra dasAn isas presentation on .net framework 2.0 by vikash chandra das
An isas presentation on .net framework 2.0 by vikash chandra das
 
Programming in c#
Programming in c#Programming in c#
Programming in c#
 
Dependency injection
Dependency injectionDependency injection
Dependency injection
 
Electron
ElectronElectron
Electron
 
.Net Framework Introduction
.Net Framework Introduction.Net Framework Introduction
.Net Framework Introduction
 
Introduction to .NET Programming
Introduction to .NET ProgrammingIntroduction to .NET Programming
Introduction to .NET Programming
 
Architecture of .net framework
Architecture of .net frameworkArchitecture of .net framework
Architecture of .net framework
 
Best practices with Microsoft Graph: Making your applications more performant...
Best practices with Microsoft Graph: Making your applications more performant...Best practices with Microsoft Graph: Making your applications more performant...
Best practices with Microsoft Graph: Making your applications more performant...
 
dot net technology
dot net technologydot net technology
dot net technology
 
MongoDB World 2019: Look, Ma, No Servers! Serverless Application Development ...
MongoDB World 2019: Look, Ma, No Servers! Serverless Application Development ...MongoDB World 2019: Look, Ma, No Servers! Serverless Application Development ...
MongoDB World 2019: Look, Ma, No Servers! Serverless Application Development ...
 
Part i
Part iPart i
Part i
 
apidays LIVE Australia 2020 - Evaluating the usability of security APIs by Dr...
apidays LIVE Australia 2020 - Evaluating the usability of security APIs by Dr...apidays LIVE Australia 2020 - Evaluating the usability of security APIs by Dr...
apidays LIVE Australia 2020 - Evaluating the usability of security APIs by Dr...
 
Reactive Microservices with Quarkus
Reactive Microservices with QuarkusReactive Microservices with Quarkus
Reactive Microservices with Quarkus
 

Ähnlich wie Igor Kochetov "What is wrong with Dependency Injection? Myths and Truths"

49.INS2065.Computer Based Technologies.TA.NguyenDucAnh.pdf
49.INS2065.Computer Based Technologies.TA.NguyenDucAnh.pdf49.INS2065.Computer Based Technologies.TA.NguyenDucAnh.pdf
49.INS2065.Computer Based Technologies.TA.NguyenDucAnh.pdf
cNguyn506241
 
Meetup Devops-Geneva-19.10.2019
Meetup Devops-Geneva-19.10.2019Meetup Devops-Geneva-19.10.2019
Meetup Devops-Geneva-19.10.2019
Hidora
 

Ähnlich wie Igor Kochetov "What is wrong with Dependency Injection? Myths and Truths" (20)

Cytoscape CI Chapter 2
Cytoscape CI Chapter 2Cytoscape CI Chapter 2
Cytoscape CI Chapter 2
 
Top 50 .NET Interview Questions and Answers 2019 | Edureka
Top 50 .NET Interview Questions and Answers 2019 | EdurekaTop 50 .NET Interview Questions and Answers 2019 | Edureka
Top 50 .NET Interview Questions and Answers 2019 | Edureka
 
49.INS2065.Computer Based Technologies.TA.NguyenDucAnh.pdf
49.INS2065.Computer Based Technologies.TA.NguyenDucAnh.pdf49.INS2065.Computer Based Technologies.TA.NguyenDucAnh.pdf
49.INS2065.Computer Based Technologies.TA.NguyenDucAnh.pdf
 
Docker Bday #5, SF Edition: Introduction to Docker
Docker Bday #5, SF Edition: Introduction to DockerDocker Bday #5, SF Edition: Introduction to Docker
Docker Bday #5, SF Edition: Introduction to Docker
 
Bahrain ch9 introduction to docker 5th birthday
Bahrain ch9 introduction to docker 5th birthday Bahrain ch9 introduction to docker 5th birthday
Bahrain ch9 introduction to docker 5th birthday
 
MOUG17: Oracle APEX - Tame IT Backlog Low Code Micro Apps in APEX
MOUG17: Oracle APEX - Tame IT Backlog Low Code Micro Apps in APEXMOUG17: Oracle APEX - Tame IT Backlog Low Code Micro Apps in APEX
MOUG17: Oracle APEX - Tame IT Backlog Low Code Micro Apps in APEX
 
Oracle CNCF Meetup Malaga - Build CNCF projects in OCI
Oracle CNCF Meetup Malaga - Build CNCF projects in OCIOracle CNCF Meetup Malaga - Build CNCF projects in OCI
Oracle CNCF Meetup Malaga - Build CNCF projects in OCI
 
Reactive Microservices with Spring 5: WebFlux
Reactive Microservices with Spring 5: WebFlux Reactive Microservices with Spring 5: WebFlux
Reactive Microservices with Spring 5: WebFlux
 
SDN :: Software Defined Networking –2017 Executive Overview
SDN :: Software Defined Networking –2017 Executive OverviewSDN :: Software Defined Networking –2017 Executive Overview
SDN :: Software Defined Networking –2017 Executive Overview
 
Docker Birthday #5 Meetup Cluj - Presentation
Docker Birthday #5 Meetup Cluj - PresentationDocker Birthday #5 Meetup Cluj - Presentation
Docker Birthday #5 Meetup Cluj - Presentation
 
Tampere Docker meetup - Happy 5th Birthday Docker
Tampere Docker meetup - Happy 5th Birthday DockerTampere Docker meetup - Happy 5th Birthday Docker
Tampere Docker meetup - Happy 5th Birthday Docker
 
"How to create an infrastructure in .NET", Leonid Chetverikov
"How to create an infrastructure in .NET", Leonid Chetverikov"How to create an infrastructure in .NET", Leonid Chetverikov
"How to create an infrastructure in .NET", Leonid Chetverikov
 
Java Programming
Java ProgrammingJava Programming
Java Programming
 
Interoperable Clouds and How to Build (or Buy) Them
Interoperable Clouds and How to Build (or Buy) ThemInteroperable Clouds and How to Build (or Buy) Them
Interoperable Clouds and How to Build (or Buy) Them
 
Meetup Devops-Geneva-19.10.2019
Meetup Devops-Geneva-19.10.2019Meetup Devops-Geneva-19.10.2019
Meetup Devops-Geneva-19.10.2019
 
Deploying deep learning models with Docker and Kubernetes
Deploying deep learning models with Docker and KubernetesDeploying deep learning models with Docker and Kubernetes
Deploying deep learning models with Docker and Kubernetes
 
Build Event-Driven Microservices with Confluent Cloud Workshop #1
Build Event-Driven Microservices with Confluent Cloud Workshop #1Build Event-Driven Microservices with Confluent Cloud Workshop #1
Build Event-Driven Microservices with Confluent Cloud Workshop #1
 
OCCIware presentation at EclipseDay in Lyon, November 2017, by Marc Dutoo, Smile
OCCIware presentation at EclipseDay in Lyon, November 2017, by Marc Dutoo, SmileOCCIware presentation at EclipseDay in Lyon, November 2017, by Marc Dutoo, Smile
OCCIware presentation at EclipseDay in Lyon, November 2017, by Marc Dutoo, Smile
 
Model and pilot all cloud layers with OCCIware - Eclipse Day Lyon 2017
Model and pilot all cloud layers with OCCIware - Eclipse Day Lyon 2017Model and pilot all cloud layers with OCCIware - Eclipse Day Lyon 2017
Model and pilot all cloud layers with OCCIware - Eclipse Day Lyon 2017
 
DevOps Days Boston 2017: Real-world Kubernetes for DevOps
DevOps Days Boston 2017: Real-world Kubernetes for DevOpsDevOps Days Boston 2017: Real-world Kubernetes for DevOps
DevOps Days Boston 2017: Real-world Kubernetes for DevOps
 

Mehr von Fwdays

Mehr von Fwdays (20)

"How Preply reduced ML model development time from 1 month to 1 day",Yevhen Y...
"How Preply reduced ML model development time from 1 month to 1 day",Yevhen Y..."How Preply reduced ML model development time from 1 month to 1 day",Yevhen Y...
"How Preply reduced ML model development time from 1 month to 1 day",Yevhen Y...
 
"GenAI Apps: Our Journey from Ideas to Production Excellence",Danil Topchii
"GenAI Apps: Our Journey from Ideas to Production Excellence",Danil Topchii"GenAI Apps: Our Journey from Ideas to Production Excellence",Danil Topchii
"GenAI Apps: Our Journey from Ideas to Production Excellence",Danil Topchii
 
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks..."LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
 
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
 
"What is a RAG system and how to build it",Dmytro Spodarets
"What is a RAG system and how to build it",Dmytro Spodarets"What is a RAG system and how to build it",Dmytro Spodarets
"What is a RAG system and how to build it",Dmytro Spodarets
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
 
"Distributed graphs and microservices in Prom.ua", Maksym Kindritskyi
"Distributed graphs and microservices in Prom.ua",  Maksym Kindritskyi"Distributed graphs and microservices in Prom.ua",  Maksym Kindritskyi
"Distributed graphs and microservices in Prom.ua", Maksym Kindritskyi
 
"Rethinking the existing data loading and processing process as an ETL exampl...
"Rethinking the existing data loading and processing process as an ETL exampl..."Rethinking the existing data loading and processing process as an ETL exampl...
"Rethinking the existing data loading and processing process as an ETL exampl...
 
"How Ukrainian IT specialist can go on vacation abroad without crossing the T...
"How Ukrainian IT specialist can go on vacation abroad without crossing the T..."How Ukrainian IT specialist can go on vacation abroad without crossing the T...
"How Ukrainian IT specialist can go on vacation abroad without crossing the T...
 
"The Strength of Being Vulnerable: the experience from CIA, Tesla and Uber", ...
"The Strength of Being Vulnerable: the experience from CIA, Tesla and Uber", ..."The Strength of Being Vulnerable: the experience from CIA, Tesla and Uber", ...
"The Strength of Being Vulnerable: the experience from CIA, Tesla and Uber", ...
 
"[QUICK TALK] Radical candor: how to achieve results faster thanks to a cultu...
"[QUICK TALK] Radical candor: how to achieve results faster thanks to a cultu..."[QUICK TALK] Radical candor: how to achieve results faster thanks to a cultu...
"[QUICK TALK] Radical candor: how to achieve results faster thanks to a cultu...
 
"[QUICK TALK] PDP Plan, the only one door to raise your salary and boost care...
"[QUICK TALK] PDP Plan, the only one door to raise your salary and boost care..."[QUICK TALK] PDP Plan, the only one door to raise your salary and boost care...
"[QUICK TALK] PDP Plan, the only one door to raise your salary and boost care...
 
"4 horsemen of the apocalypse of working relationships (+ antidotes to them)"...
"4 horsemen of the apocalypse of working relationships (+ antidotes to them)"..."4 horsemen of the apocalypse of working relationships (+ antidotes to them)"...
"4 horsemen of the apocalypse of working relationships (+ antidotes to them)"...
 
"Reconnecting with Purpose: Rediscovering Job Interest after Burnout", Anast...
"Reconnecting with Purpose: Rediscovering Job Interest after Burnout",  Anast..."Reconnecting with Purpose: Rediscovering Job Interest after Burnout",  Anast...
"Reconnecting with Purpose: Rediscovering Job Interest after Burnout", Anast...
 
"Mentoring 101: How to effectively invest experience in the success of others...
"Mentoring 101: How to effectively invest experience in the success of others..."Mentoring 101: How to effectively invest experience in the success of others...
"Mentoring 101: How to effectively invest experience in the success of others...
 
"Mission (im) possible: How to get an offer in 2024?", Oleksandra Myronova
"Mission (im) possible: How to get an offer in 2024?",  Oleksandra Myronova"Mission (im) possible: How to get an offer in 2024?",  Oleksandra Myronova
"Mission (im) possible: How to get an offer in 2024?", Oleksandra Myronova
 
"Why have we learned how to package products, but not how to 'package ourselv...
"Why have we learned how to package products, but not how to 'package ourselv..."Why have we learned how to package products, but not how to 'package ourselv...
"Why have we learned how to package products, but not how to 'package ourselv...
 
"How to tame the dragon, or leadership with imposter syndrome", Oleksandr Zin...
"How to tame the dragon, or leadership with imposter syndrome", Oleksandr Zin..."How to tame the dragon, or leadership with imposter syndrome", Oleksandr Zin...
"How to tame the dragon, or leadership with imposter syndrome", Oleksandr Zin...
 

Kürzlich hochgeladen

Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
vu2urc
 

Kürzlich hochgeladen (20)

Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
Tech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdfTech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdf
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
Developing An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of BrazilDeveloping An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of Brazil
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 

Igor Kochetov "What is wrong with Dependency Injection? Myths and Truths"

  • 1. What is wrong with Dependency Injection? Igor Kochetov (@ k04a) Unity Technologies
  • 2. @k04a The DI book 2 Authored by: Mark “ploeh” Seemann Steven “.NET Junkie” van Deursen
  • 3. Who am I? Dev (C#, Python) in Opsy (Docker, k8s, GCP) world Attendee: NDC (Oslo), New Crafts (Paris) Speaker: BuildStuff (Vilnius), XP Days & .Net Fest (Kyiv) Religious about engineering practices and team culture, software design and good code Toolsmith at Unity Technologies, based in Odessa Twitter: @k04a Linkedin: Igor Kochetov
  • 4. @k04a Why we even talk about DI in 2019? There are conflicting opinions on the subject of DI Is it even still relevant? 4
  • 6. @k04a Nice one, but there are still questions... Who’s “we” who make sure you have what you need? How do we state “the need” in the code? Why one would use it? What are the benefits, actually? 6
  • 7. Known benefits Dependency Injection ❏ Reducing dependencies of components (and implementation details of those dependencies) ❏ Reusable code / swapping of components ❏ More testable code ❏ More readable code (by defining dependencies clearly) Loose coupling Isolated components are easier to: ❏ Modify, reuse or compose differently ❏ Test ❏ Reason about 7
  • 8. @k04a It is about Dependencies... Identifying dependencies in our code (and types of dependencies) Managing dependencies 8
  • 9. @k04a Programming to interfaces and D in SOLID High-level modules should not depend on low-level modules. Both should depend on abstractions. Abstractions should not depend on details. Details should depend on abstractions. 9
  • 10. @k04a Also related: who owns the abstractions? Model abstractions around domain-specific scenarios instead of low-level implementation details “Adapt” external dependencies to match your domain concepts Hexagonal architecture or “Ports & Adapters”: 10
  • 11. @k04a Programming to interfaces and D in SOLID ServiceX is still tightly coupled to ServiceY! 11
  • 12. How do we state “the need” in the code? 12
  • 13. @k04a Resolve dependencies using Service Locator At first, looks like very good idea, which makes it even more dangerous. But now we introduce new dependency (on the container) We also hide actual dependencies How would you write tests for it? 13
  • 14. @k04a Constructor Injection is the way to go ❏ Explicitly lists dependencies ❏ Gives clues about what class might do ❏ Also an indicator of possible SRP violations 14
  • 16. Who’s “we” who make sure you have what you need? Or “Mommy and Daddy providing a drink” 16
  • 17. @k04a Many platforms provide out-of-the box DI capabilities .NET MVC / .NET Core MVC Spring in Java Angular JS Kind of making it a de-facto standard 17
  • 18. Myth: DI is a software library or a framework Or at least requires one to be done properly @k04a 18
  • 19. @k04a IoC Container or DI Container Container - software library that ‘resolves dependencies’ and manages their ‘lifetime’ ❏ Simplifies wiring and reduces maintenance ❏ Simplifies testing ❏ Allows for dynamic configuration ❏ Supports AOP But… Is it? 19
  • 21. @k04a Composition Root and Pure DI Creating all the stuff ‘normally’ in Composition Root (console app example) 21
  • 22. @k04a Is it OK to have all the components in the app root? It is good idea to keep individual components separated Until we have to actually assemble working application 22
  • 23. @k04a Explicit is better than implicit (Zen of Python) Cost of writing vs cost of reading (or imagine bringing a new member of the team into the codebase) Let IDE and code analysis tools be your friends It is actually IMPORTANT to know, what your app does (well-structured Composition Root serves as documentation providing bird-eye view on high-level features and use- cases) 23
  • 24. @k04a Composing within framework (.Net Core MVC) As easy as registering a custom IControllerActivator 24
  • 25. @k04a Composing within framework (.Net Core MVC) … and implementing it 25
  • 26. @k04a Use-case for Aspect-Oriented-Programming Applying cross-cutting concerns (also known as non-functional requirements) like: ❏ Logging ❏ Caching ❏ Security 26
  • 27. @k04a The power of Decorators (or AOP done right) Wrap logging around any actual implementation using Decorator design pattern 27
  • 28. @k04a Injecting decorated dependency Wrap any number of cross-cutting concerns around actual implementation 28
  • 29. @k04a Applying SOLID to extreme pushes you toward FP A lot of single-method interfaces and small focused classes (implementations) Which is good, until you have to write Decorators for every of them 29
  • 30. @k04a CQS and Parameter Object Single generic abstraction Implementation consists of two types: ❏ Command ❏ CommandHandler 30
  • 31. @k04a Decorator over generic ICommandHandler Now every ‘aspect’ becomes single Decorator over generic ICommandHandler <TCommand> interface 31
  • 32. @k04a Decorator over generic ICommandHandler Write once, use for all kinds of Commands 32
  • 33. @k04a Conclusions & takeaways Container is a dependency itself It also might be a sign your codebase grew too big (read “monolith”) DI ≠ Container DI is useful as a design pattern and allows greater flexibility and more manageable codebase (easier to test, modify and reason about) 33
  • 34. @k04a The big idea (or what those have in common?) ❏ Dependency Injection (DI) ❏ Continuous Integration (CI) / Continuous Deployment (CD) ❏ Scrum, Lean or any Agile methodology / framework of your choice ❏ DevOps Answer: Those were meant to be PRACTICES (not Tools) 34