Dependency Injection Lightning Talk

Jon Bachelor
Jon BachelorSoftware Developer at Bridgepoint Education
…and you: So happy together 
 Here’s the absolute worst explanation ever:
 ”Instead of newing up objects, you just call ‘container.resolve’”
 With the definition of, “just call container.Resolve instead of newing an
object up”, when you go to a job interview and whiteboard some pseudo-
code, they might say something like:
Pretty sure this
guy voted ‘NO’
on hiring me…
 Merriam Webster Dictionary: “something that is dependent on something else”
 For example: “A time-traveling DeLorean’s dependency on plutonium was a real
problem in 1955.”
 Wikipedia’s definition relative to computer science: “[A dependency is a] coupling,
a state in which one object uses a function of another object.”
 For example: My app needs to make calls to a web service to get and store data, so my
business logic has a dependency on the web service.
 When you have a “coupling” of two classes, a loose coupling is the way to go in order to
make your code more testable and maintainable for the future.
 3 common types of dependency injection:
 Setter Injection
 Dependencies are injected through specific property setters. I have never seen this in the wild.
 Interface Injection
 Classes with dependencies implement an interface that exposes a setter method, and
dependencies use this to inject themselves into any class passed to it. I have never seen this in
the wild.
 Constructor Injection
 Yaaaaaaay! Winner winner!! Dependencies are injected through the class constructor. This is the
only method I’ve seen in the wild.
Interface: A
contract or
specification
Concrete
implementation of
the interface
 We want ALL of our dependencies to be based on an interface.
 Any service code you write should have an interface. Other classes in your app
will rely only on the interface… Not the “concrete” implementation of the
interface.
 You will definitely run into situations where some 3rd party code does not provide
an interface (Microsoft’s HttpClient class, as a ubiquitous for-instance).
 Never give up… Never surrender! There are ways to create wrappers for such classes
which keep the vast majority of your code testable and loosely coupled. True story!
 Before DI  After DI
 You’ll notice you use the “new”
keyword a WHOLE lot less when
using Dependency Injection.
 When I finally learned how to use dependency injection, I still had no clue WHY
we were using it. All I knew was that some highly paid consultants said to do it,
and that was that.
 The two most important reasons to use “DI” (Dependency Injection):
 Makes it super-mega-monster-easy to unit test code without insanely complicated
setups.
 Caveat: As long as you learn how to use mocks when unit testing. On Luxor, we use MOQ, which
I would absolutely recommend.
 Another popular mocking framework for .NET: NSubstitute
 Promotes writing code where various services (aka ‘dependencies’) your code requires
can be loosely coupled with your code.
 The container is at the very heart of your DI implementation.
 Interfaces are mapped to concrete types, so that the container can provide the
correct implementation when an interface is requested.
 Containers typically allow you to specify the “lifetime” of any object it knows
about. You can have a singleton, one instance per thread, and more.
 Containers map views to viewModels in the MVVM design pattern (used in UWP,
WPF, and Xamarin.Forms apps, for example).
 The container is the thing that will new up all of your services, views, and
viewModels as they are needed.
 The magic: When you need some service in a class, you just add the interface to
its constructor, and the container magically will provide it!
 No… SERIOUSLY. It’s kind of amazing!!
 How does the
container know what
concrete types to
provide for your
interfaces?
 How does the
container know which
views go with which
viewModels?
 Answer:
Registration!
 Notice that ALL of the constructor parameters are interface types.
 When we discover we need a new service, we can just add it to the constructor,
and our DI container takes care of injecting it (thanks to the registration).
 Note: Yes, there might be a code smell of too many dependencies in this ViewModel, but
that’s not important right now. 
 The ViewModel from the previous slide has SEVEN dependencies. That would be
a TON of overhead if they were all concrete classes, and LOTS of integration
concerns to worry about when it came to testing!
 Instead, we just mock, mock, mock all our problems away!
Arrange
Act
Assert
 Infinitely easier (and possible!) unit testing of code.
 Loosely coupled software.
 While DI is complex, the alternative is even MORE complex!
 Just ask for whatever services you want in a constructor, and your DI container
will “make it so”!
 No complications about whether a class is a singleton or not… You already took
care of that when you registered the class with the container.
 As long as the container knows about the interface and the concrete class you
want to implement it with, you’re all set.
 Swap out implementations of interfaces (demo mode, anyone??).
Dependency Injection Lightning Talk
1 of 17

Recommended

Welcome to React & Flux ! by
Welcome to React & Flux !Welcome to React & Flux !
Welcome to React & Flux !Ritesh Kumar
548 views28 slides
C# classes by
C#   classesC#   classes
C# classesTiago
310 views207 slides
Large-Scale JavaScript Development by
Large-Scale JavaScript DevelopmentLarge-Scale JavaScript Development
Large-Scale JavaScript DevelopmentAddy Osmani
5.3K views48 slides
Silverlight difference faqs- 2 by
Silverlight difference faqs- 2Silverlight difference faqs- 2
Silverlight difference faqs- 2Umar Ali
368 views4 slides
29 Essential AngularJS Interview Questions by
29 Essential AngularJS Interview Questions29 Essential AngularJS Interview Questions
29 Essential AngularJS Interview QuestionsArc & Codementor
487.6K views31 slides
Building Large Scale Javascript Application by
Building Large Scale Javascript ApplicationBuilding Large Scale Javascript Application
Building Large Scale Javascript ApplicationAnis Ahmad
16K views81 slides

More Related Content

Similar to Dependency Injection Lightning Talk

Introduction to Docker and Containers- Learning Simple by
Introduction to Docker and Containers- Learning SimpleIntroduction to Docker and Containers- Learning Simple
Introduction to Docker and Containers- Learning SimpleSandeep Hijam
54 views15 slides
Evolve Your Code by
Evolve Your CodeEvolve Your Code
Evolve Your CodeRookieOne
618 views79 slides
Dependency Injection and Autofac by
Dependency Injection and AutofacDependency Injection and Autofac
Dependency Injection and Autofacmeghantaylor
5.3K views27 slides
Java Programming by
Java ProgrammingJava Programming
Java ProgrammingTracy Clark
3 views153 slides
C# interview by
C# interviewC# interview
C# interviewajeesharakkal
1.8K views18 slides
The Taming Of The Code by
The Taming Of The CodeThe Taming Of The Code
The Taming Of The CodeAlan Stevens
364 views39 slides

Similar to Dependency Injection Lightning Talk(20)

Introduction to Docker and Containers- Learning Simple by Sandeep Hijam
Introduction to Docker and Containers- Learning SimpleIntroduction to Docker and Containers- Learning Simple
Introduction to Docker and Containers- Learning Simple
Sandeep Hijam54 views
Evolve Your Code by RookieOne
Evolve Your CodeEvolve Your Code
Evolve Your Code
RookieOne618 views
Dependency Injection and Autofac by meghantaylor
Dependency Injection and AutofacDependency Injection and Autofac
Dependency Injection and Autofac
meghantaylor5.3K views
The Taming Of The Code by Alan Stevens
The Taming Of The CodeThe Taming Of The Code
The Taming Of The Code
Alan Stevens364 views
Lotusphere 2007 AD507 Leveraging the Power of Object Oriented Programming in ... by Bill Buchan
Lotusphere 2007 AD507 Leveraging the Power of Object Oriented Programming in ...Lotusphere 2007 AD507 Leveraging the Power of Object Oriented Programming in ...
Lotusphere 2007 AD507 Leveraging the Power of Object Oriented Programming in ...
Bill Buchan470 views
Building Rich User Experiences Without JavaScript Spaghetti by Jared Faris
Building Rich User Experiences Without JavaScript SpaghettiBuilding Rich User Experiences Without JavaScript Spaghetti
Building Rich User Experiences Without JavaScript Spaghetti
Jared Faris1.5K views
Devops interview questions 1 www.bigclasses.com by bigclasses.com
Devops interview questions  1  www.bigclasses.comDevops interview questions  1  www.bigclasses.com
Devops interview questions 1 www.bigclasses.com
bigclasses.com 152 views
SELJE_Database_Unit_Testing.pdf by Eric Selje
SELJE_Database_Unit_Testing.pdfSELJE_Database_Unit_Testing.pdf
SELJE_Database_Unit_Testing.pdf
Eric Selje4 views
Dependency Injection in iOS by Pablo Villar
Dependency Injection in iOSDependency Injection in iOS
Dependency Injection in iOS
Pablo Villar627 views
Working Effectively With Legacy Code by Naresh Jain
Working Effectively With Legacy CodeWorking Effectively With Legacy Code
Working Effectively With Legacy Code
Naresh Jain56.3K views
Elements of DDD with ASP.NET MVC & Entity Framework Code First by Enea Gabriel
Elements of DDD with ASP.NET MVC & Entity Framework Code FirstElements of DDD with ASP.NET MVC & Entity Framework Code First
Elements of DDD with ASP.NET MVC & Entity Framework Code First
Enea Gabriel7.6K views
Poco Es Mucho: WCF, EF, and Class Design by James Phillips
Poco Es Mucho: WCF, EF, and Class DesignPoco Es Mucho: WCF, EF, and Class Design
Poco Es Mucho: WCF, EF, and Class Design
James Phillips812 views
So Many Frameworks, So Little Time by Richard Lord
So Many Frameworks, So Little TimeSo Many Frameworks, So Little Time
So Many Frameworks, So Little Time
Richard Lord579 views

Recently uploaded

DSD-INT 2023 Leveraging the results of a 3D hydrodynamic model to improve the... by
DSD-INT 2023 Leveraging the results of a 3D hydrodynamic model to improve the...DSD-INT 2023 Leveraging the results of a 3D hydrodynamic model to improve the...
DSD-INT 2023 Leveraging the results of a 3D hydrodynamic model to improve the...Deltares
6 views22 slides
DSD-INT 2023 Salt intrusion Modelling of the Lauwersmeer, towards a measureme... by
DSD-INT 2023 Salt intrusion Modelling of the Lauwersmeer, towards a measureme...DSD-INT 2023 Salt intrusion Modelling of the Lauwersmeer, towards a measureme...
DSD-INT 2023 Salt intrusion Modelling of the Lauwersmeer, towards a measureme...Deltares
5 views28 slides
Headless JS UG Presentation.pptx by
Headless JS UG Presentation.pptxHeadless JS UG Presentation.pptx
Headless JS UG Presentation.pptxJack Spektor
7 views24 slides
Copilot Prompting Toolkit_All Resources.pdf by
Copilot Prompting Toolkit_All Resources.pdfCopilot Prompting Toolkit_All Resources.pdf
Copilot Prompting Toolkit_All Resources.pdfRiccardo Zamana
8 views4 slides
Team Transformation Tactics for Holistic Testing and Quality (Japan Symposium... by
Team Transformation Tactics for Holistic Testing and Quality (Japan Symposium...Team Transformation Tactics for Holistic Testing and Quality (Japan Symposium...
Team Transformation Tactics for Holistic Testing and Quality (Japan Symposium...Lisi Hocke
28 views124 slides
SAP FOR CONTRACT MANUFACTURING.pdf by
SAP FOR CONTRACT MANUFACTURING.pdfSAP FOR CONTRACT MANUFACTURING.pdf
SAP FOR CONTRACT MANUFACTURING.pdfVirendra Rai, PMP
11 views2 slides

Recently uploaded(20)

DSD-INT 2023 Leveraging the results of a 3D hydrodynamic model to improve the... by Deltares
DSD-INT 2023 Leveraging the results of a 3D hydrodynamic model to improve the...DSD-INT 2023 Leveraging the results of a 3D hydrodynamic model to improve the...
DSD-INT 2023 Leveraging the results of a 3D hydrodynamic model to improve the...
Deltares6 views
DSD-INT 2023 Salt intrusion Modelling of the Lauwersmeer, towards a measureme... by Deltares
DSD-INT 2023 Salt intrusion Modelling of the Lauwersmeer, towards a measureme...DSD-INT 2023 Salt intrusion Modelling of the Lauwersmeer, towards a measureme...
DSD-INT 2023 Salt intrusion Modelling of the Lauwersmeer, towards a measureme...
Deltares5 views
Headless JS UG Presentation.pptx by Jack Spektor
Headless JS UG Presentation.pptxHeadless JS UG Presentation.pptx
Headless JS UG Presentation.pptx
Jack Spektor7 views
Copilot Prompting Toolkit_All Resources.pdf by Riccardo Zamana
Copilot Prompting Toolkit_All Resources.pdfCopilot Prompting Toolkit_All Resources.pdf
Copilot Prompting Toolkit_All Resources.pdf
Riccardo Zamana8 views
Team Transformation Tactics for Holistic Testing and Quality (Japan Symposium... by Lisi Hocke
Team Transformation Tactics for Holistic Testing and Quality (Japan Symposium...Team Transformation Tactics for Holistic Testing and Quality (Japan Symposium...
Team Transformation Tactics for Holistic Testing and Quality (Japan Symposium...
Lisi Hocke28 views
2023-November-Schneider Electric-Meetup-BCN Admin Group.pptx by animuscrm
2023-November-Schneider Electric-Meetup-BCN Admin Group.pptx2023-November-Schneider Electric-Meetup-BCN Admin Group.pptx
2023-November-Schneider Electric-Meetup-BCN Admin Group.pptx
animuscrm13 views
DSD-INT 2023 Thermobaricity in 3D DCSM-FM - taking pressure into account in t... by Deltares
DSD-INT 2023 Thermobaricity in 3D DCSM-FM - taking pressure into account in t...DSD-INT 2023 Thermobaricity in 3D DCSM-FM - taking pressure into account in t...
DSD-INT 2023 Thermobaricity in 3D DCSM-FM - taking pressure into account in t...
Deltares9 views
SUGCON ANZ Presentation V2.1 Final.pptx by Jack Spektor
SUGCON ANZ Presentation V2.1 Final.pptxSUGCON ANZ Presentation V2.1 Final.pptx
SUGCON ANZ Presentation V2.1 Final.pptx
Jack Spektor22 views
Tridens DevOps by Tridens
Tridens DevOpsTridens DevOps
Tridens DevOps
Tridens9 views
DSD-INT 2023 3D hydrodynamic modelling of microplastic transport in lakes - J... by Deltares
DSD-INT 2023 3D hydrodynamic modelling of microplastic transport in lakes - J...DSD-INT 2023 3D hydrodynamic modelling of microplastic transport in lakes - J...
DSD-INT 2023 3D hydrodynamic modelling of microplastic transport in lakes - J...
Deltares9 views
DSD-INT 2023 Machine learning in hydraulic engineering - Exploring unseen fut... by Deltares
DSD-INT 2023 Machine learning in hydraulic engineering - Exploring unseen fut...DSD-INT 2023 Machine learning in hydraulic engineering - Exploring unseen fut...
DSD-INT 2023 Machine learning in hydraulic engineering - Exploring unseen fut...
Deltares6 views
DSD-INT 2023 Delft3D FM Suite 2024.01 1D2D - Beta testing programme - Geertsema by Deltares
DSD-INT 2023 Delft3D FM Suite 2024.01 1D2D - Beta testing programme - GeertsemaDSD-INT 2023 Delft3D FM Suite 2024.01 1D2D - Beta testing programme - Geertsema
DSD-INT 2023 Delft3D FM Suite 2024.01 1D2D - Beta testing programme - Geertsema
Deltares17 views
.NET Developer Conference 2023 - .NET Microservices mit Dapr – zu viel Abstra... by Marc Müller
.NET Developer Conference 2023 - .NET Microservices mit Dapr – zu viel Abstra....NET Developer Conference 2023 - .NET Microservices mit Dapr – zu viel Abstra...
.NET Developer Conference 2023 - .NET Microservices mit Dapr – zu viel Abstra...
Marc Müller38 views
Dev-HRE-Ops - Addressing the _Last Mile DevOps Challenge_ in Highly Regulated... by TomHalpin9
Dev-HRE-Ops - Addressing the _Last Mile DevOps Challenge_ in Highly Regulated...Dev-HRE-Ops - Addressing the _Last Mile DevOps Challenge_ in Highly Regulated...
Dev-HRE-Ops - Addressing the _Last Mile DevOps Challenge_ in Highly Regulated...
TomHalpin95 views
DSD-INT 2023 Simulating a falling apron in Delft3D 4 - Engineering Practice -... by Deltares
DSD-INT 2023 Simulating a falling apron in Delft3D 4 - Engineering Practice -...DSD-INT 2023 Simulating a falling apron in Delft3D 4 - Engineering Practice -...
DSD-INT 2023 Simulating a falling apron in Delft3D 4 - Engineering Practice -...
Deltares6 views
20231129 - Platform @ localhost 2023 - Application-driven infrastructure with... by sparkfabrik
20231129 - Platform @ localhost 2023 - Application-driven infrastructure with...20231129 - Platform @ localhost 2023 - Application-driven infrastructure with...
20231129 - Platform @ localhost 2023 - Application-driven infrastructure with...
sparkfabrik5 views

Dependency Injection Lightning Talk

  • 1. …and you: So happy together 
  • 2.  Here’s the absolute worst explanation ever:  ”Instead of newing up objects, you just call ‘container.resolve’”
  • 3.  With the definition of, “just call container.Resolve instead of newing an object up”, when you go to a job interview and whiteboard some pseudo- code, they might say something like: Pretty sure this guy voted ‘NO’ on hiring me…
  • 4.  Merriam Webster Dictionary: “something that is dependent on something else”  For example: “A time-traveling DeLorean’s dependency on plutonium was a real problem in 1955.”  Wikipedia’s definition relative to computer science: “[A dependency is a] coupling, a state in which one object uses a function of another object.”  For example: My app needs to make calls to a web service to get and store data, so my business logic has a dependency on the web service.  When you have a “coupling” of two classes, a loose coupling is the way to go in order to make your code more testable and maintainable for the future.
  • 5.  3 common types of dependency injection:  Setter Injection  Dependencies are injected through specific property setters. I have never seen this in the wild.  Interface Injection  Classes with dependencies implement an interface that exposes a setter method, and dependencies use this to inject themselves into any class passed to it. I have never seen this in the wild.  Constructor Injection  Yaaaaaaay! Winner winner!! Dependencies are injected through the class constructor. This is the only method I’ve seen in the wild.
  • 7.  We want ALL of our dependencies to be based on an interface.  Any service code you write should have an interface. Other classes in your app will rely only on the interface… Not the “concrete” implementation of the interface.  You will definitely run into situations where some 3rd party code does not provide an interface (Microsoft’s HttpClient class, as a ubiquitous for-instance).  Never give up… Never surrender! There are ways to create wrappers for such classes which keep the vast majority of your code testable and loosely coupled. True story!
  • 8.  Before DI  After DI  You’ll notice you use the “new” keyword a WHOLE lot less when using Dependency Injection.
  • 9.  When I finally learned how to use dependency injection, I still had no clue WHY we were using it. All I knew was that some highly paid consultants said to do it, and that was that.  The two most important reasons to use “DI” (Dependency Injection):  Makes it super-mega-monster-easy to unit test code without insanely complicated setups.  Caveat: As long as you learn how to use mocks when unit testing. On Luxor, we use MOQ, which I would absolutely recommend.  Another popular mocking framework for .NET: NSubstitute  Promotes writing code where various services (aka ‘dependencies’) your code requires can be loosely coupled with your code.
  • 10.  The container is at the very heart of your DI implementation.  Interfaces are mapped to concrete types, so that the container can provide the correct implementation when an interface is requested.  Containers typically allow you to specify the “lifetime” of any object it knows about. You can have a singleton, one instance per thread, and more.  Containers map views to viewModels in the MVVM design pattern (used in UWP, WPF, and Xamarin.Forms apps, for example).  The container is the thing that will new up all of your services, views, and viewModels as they are needed.
  • 11.  The magic: When you need some service in a class, you just add the interface to its constructor, and the container magically will provide it!  No… SERIOUSLY. It’s kind of amazing!!
  • 12.  How does the container know what concrete types to provide for your interfaces?  How does the container know which views go with which viewModels?  Answer: Registration!
  • 13.  Notice that ALL of the constructor parameters are interface types.  When we discover we need a new service, we can just add it to the constructor, and our DI container takes care of injecting it (thanks to the registration).  Note: Yes, there might be a code smell of too many dependencies in this ViewModel, but that’s not important right now. 
  • 14.  The ViewModel from the previous slide has SEVEN dependencies. That would be a TON of overhead if they were all concrete classes, and LOTS of integration concerns to worry about when it came to testing!  Instead, we just mock, mock, mock all our problems away!
  • 16.  Infinitely easier (and possible!) unit testing of code.  Loosely coupled software.  While DI is complex, the alternative is even MORE complex!  Just ask for whatever services you want in a constructor, and your DI container will “make it so”!  No complications about whether a class is a singleton or not… You already took care of that when you registered the class with the container.  As long as the container knows about the interface and the concrete class you want to implement it with, you’re all set.  Swap out implementations of interfaces (demo mode, anyone??).