SlideShare a Scribd company logo
http://www.depthconsulting.ca
http://www.depthconsulting.ca
Developer
Fundamentals
:
The Series
Calgary
.NET User
Group
Dependency Injection
What You Need to Know
Dave White
Microsoft MVP – Visual Studio ALM
http://www.depthconsulting.ca
Developer Fundamentals Series
Testing – Nov. 26, 2014
SOLID – Feb. 26, 2015
Dependency Injection – April 22, 2015
Source Control – Branching/Merging – June
Putting It All Together – July
http://www.depthconsulting.ca
Dave White
Principal Consultant – Depth Consulting
http://linkd.in/giMxuw
@AgileRamblings
• In IT for over 15 years
• Microsoft MVP – Visual Studio ALM (2 years)
• Kanban/Agile Evangelist, Speaker
• PRDC 2011, 2012, LKUK, Local events
• Program Director – KCP Program - LKU
• Professional Scrum Master
• Microsoft Platform Developer (C#, Web, WPF)
http://www.agileramblings.com
http://www.depthconsulting.ca
Simon Timms
Senior .NET Dev – Pacesetter Directional Drilling
http://linkd.in/15pYAWL
@stimms
• In IT for over a decade (+/− few months)
• Microsoft MVP – ASP.NET/IIS (2 years)
• Prolific Speaker and Community Involvement
• PRDC (many), AzureCamp, Local events
• Calgary .NET User Group – President
• Author
• Loves Development
• Polyglot
• Web apps, .NET back ends
• Visualization and Cloud
http://blog.simontimms.com/about/
http://www.depthconsulting.ca
WHY?
http://www.depthconsulting.ca
Why We Don’t Inject
• I don’t test design
• No one around me is
doing it
• Too hard to learn
• No tools
• My stuff doesn’t
need it
• Our code is horrible
and very deeply
coupled
• No time
http://www.depthconsulting.ca
The truth of the matter…
• I makes testing
designing easier!
• It encourages SOLID
principles
• Everyone is doing it
• Easier than ever
• Investment in tools
• Private and OSS
• You can start small
• You will be a better
developer
• Drives growth down
maturity path
• Increasing
Complexity
• Business and technical
• It makes you faster
http://www.depthconsulting.ca
Let’s start
learning!
By John Castillo (Jack)
(https://creativecommons.org/licenses/by/2.0/deed.en),
via Wikimedia Commons
http://www.depthconsulting.ca
What is Dependency Injection (DI)
Short:
Giving an object it’s
instance variables
Medium:
Dependency injection is
providing the objects that an
object needs (its
dependencies) instead of
constructing them itself
Long:
Dependency injection is
a software design pattern in
which one or
more dependencies (or
services) are injected, or
passed by reference, into a
dependent object (or client)
and are made part of the
client's state.
http://en.wikipedia.org/wiki/Dependency_injection
James Shore - http://bit.ly/1cUUGaA
http://www.depthconsulting.ca
Two Ways to Inject
Constructor
• explicitly required
• “I cannot function without
this”
Property (Setter)
• optionally required
• “I can function without
this”
public class MyClass
{
public void MyClass(IDependentOn
dependency)
{
}
}
public class MyClass
{
public void MyClass()
{
}
public IDependentOn dependency
{get; set;}
}
http://www.depthconsulting.ca
Two Types of Things to Inject
Concrete Classes
• less flexible
• can only inject single
implementation
Interfaces
• more flexible
• can inject multiple
implementations
public class MyClass
{
public void MyClass(DependentOn
dependency)
{
}
}
public class DependentOn {}
public class MyClass
{
public void MyClass(IDependentOn
dependency)
{
}
}
public interface IDependentOn {}
public class Concrete1: IDependentOn {}
public class Concrete2: IDependentOn {}
http://www.depthconsulting.ca
Demo
http://www.depthconsulting.ca
By Deutsche Fotothek [CC BY-SA 3.0 de
(http://creativecommons.org/licenses/by-sa/3.0/de/deed.en)],
via Wikimedia Commons
That
doesn’t
look so
hard
but why
again?
http://www.depthconsulting.ca
Encourages Modularity
• Makes testing designing easier
• S in SOLID is good
• Decomposing functionality into smaller parts is
good
• Freeing yourself from some concerns which is
good
• Allows implementation flexibility (good)
• Centralized implementation (good)
• Reduces Ctrl-C / Ctrl-V code reuse (good)
http://www.depthconsulting.ca
Remember
• You are reading this because you want to
improve
• You’ve learned about testing design
• You’ve learned about SOLID
• These things should be viewed as a holistic
improvement
They all support each other
http://www.depthconsulting.ca
By Venpia (Own work) [CC-BY-SA-3.0
(http://creativecommons.org/licenses/by-sa/3.0)],
via Wikimedia Commons
So…
there are
tools to
make this
easier?
http://www.depthconsulting.ca
Dependency Injection Containers
http://lmgtfy.com/?q=dependency+injection
Castle Windsor
- Unity
StructureMap
http://www.depthconsulting.ca
What do Containers Provide
• Primary Functions
• Registration
• Resolution
• Manage Lifetime
• Secondary Functions
• Interception/AOP
• Assembly Loading/Type Discovery
http://www.depthconsulting.ca
Registration
• Enables container to know what to create
• Describes construction intent
• What concrete classes are associated with requests
• Singleton, always create new instances, instance per scope (request)
• Interception
http://www.depthconsulting.ca
Resolution
• Construct the actual instances as required
• Add interceptors (more later)
http://www.depthconsulting.ca
Manage Lifetime
• If construction is a primary responsibility,
so is destruction
http://www.depthconsulting.ca
Interception
• You can do AOP with containers
• Usually require an extension
http://www.depthconsulting.ca
Assembly Loading/Type Discovery
• Some containers have helper functions that
discover implementation classes that fit a
specific criteria
• Attributes, Interfaces implemented
http://www.depthconsulting.ca
Demo
http://www.depthconsulting.ca
Hold
on a
second
here
Mister! By Achim Hering (Own work)
[GFDL (http://www.gnu.org/copyleft/fdl.html) or
CC-BY-3.0 (http://creativecommons.org/licenses/by/3.0)]
via Wikimedia Commons
http://www.depthconsulting.ca
The Real Truth - It Can’t Be All
Roses• Dependency Inject is often a breaking
change
• Not a refactoring:
• New application architecture pattern in your application
• Burdened by a MOUNTAIN of technical debt and bad design
• It requires a better understanding of
• SOLID and the chosen container’s API
• You’re whole team has to understand it
• You may have to seek permission from your teammates
• Not managers
• Dependency graphs aren’t any more
http://www.depthconsulting.ca
The Real Truth – There ARE Roses
• Making Unit Tests will be easier
• You will create higher quality product
• You will create better designs
• Most (all?) of the container
implementations are OSS (read: free)
http://www.depthconsulting.ca
We can
do this!
By Weird Beard – Happy [CC-BY-2.0
(http://creativecommons.org/licenses/by/2.0)],
via Wikimedia Commons
http://www.depthconsulting.ca
Next Steps
• Build an example
application
• Understand what you
want from a DI
Container
• Try multiple containers
that fulfill your
requirements
• Share your learning
with your team
KEEP
CALM
and
DI/SOLID/TES
T
ON
http://www.depthconsulting.ca
Tools
• Visual Studio Community
http://www.visualstudio.com/products/visual-studio-community-vs
• Visual Studio 2015 Preview
http://www.visualstudio.com/en-us/news/vs2015-preview-vs
• Nuget
• Autofac, Moq
• http://www.autofac.org
• https://github.com/Moq/moq4
PM> Install-Package Autofac
PM> Install-Package Moq
http://www.depthconsulting.ca
These are the MeetUps you’re
looking for…
Testing – Nov. 26, 2014
SOLID – Feb. 26, 2015
Dependency Injection – April 22, 2015
Source Control – Branching/Merging – June
Putting It All Together – July
http://www.depthconsulting.ca
City of Calgary - Hackathon
http://www.depthconsulting.ca
NOW PRIZES!!
1 ReSharper licence
https://www.jetbrains.com/resharper/
1 Infragistics Ultimate License
http://www.infragistics.com/
INFRAGISTICS ULTIMATE
http://www.depthconsulting.ca
Thanks
Dave White
@agileramblings
http://www.agileramblings.co
m
dave@depthconsulting.ca
Simon Timms
@stimms
http://blog.simontimms.com
stimms@gmail.com

More Related Content

PPTX
VisionX Prototyping.
PDF
Java Framework comparison
PDF
[English version] JavaFX and Web Integration
PDF
30 Skills to Master to Become a Senior Software Engineer
PDF
Cordova: APIs and instruments
PDF
Behaviour testing for single-page applications and API’s
PDF
Developing WordPress Plugins : For Begineers
PPTX
Hybrid Mobile Development with Apache Cordova and Java EE 7 (JavaOne 2014)
VisionX Prototyping.
Java Framework comparison
[English version] JavaFX and Web Integration
30 Skills to Master to Become a Senior Software Engineer
Cordova: APIs and instruments
Behaviour testing for single-page applications and API’s
Developing WordPress Plugins : For Begineers
Hybrid Mobile Development with Apache Cordova and Java EE 7 (JavaOne 2014)

What's hot (20)

PDF
Easily extend your existing php app with an api
PDF
Containerizing legacy applications
PDF
Understanding Microservices
PDF
Continuous integration and delivery for java based web applications
PPT
Yii workshop
PPTX
What's new in Spring Boot 2.0
PPTX
JavaFX Versus HTML5 - JavaOne 2014
KEY
jQuery Conference Boston 2011 CouchApps
PPTX
ProtractorJS for automated testing of Angular 1.x/2.x applications
PDF
[2015/2016] Apache Cordova
PPTX
JavaFX and HTML5 - Like Curds and Rice
PDF
EuroPython 2011 - How to build complex web applications having fun?
PDF
Intro To Reactive Programming
KEY
Natural Language UI Testing using Behavior Driven Development with Pavlov and...
PPTX
JVx - Application framework - 2013
PPTX
02 configuration
KEY
Everything you need to know about HTML5 in 15 min
PPTX
Introducing ASP.NET Core 2.0
PDF
Intro to Reactive Programming
Easily extend your existing php app with an api
Containerizing legacy applications
Understanding Microservices
Continuous integration and delivery for java based web applications
Yii workshop
What's new in Spring Boot 2.0
JavaFX Versus HTML5 - JavaOne 2014
jQuery Conference Boston 2011 CouchApps
ProtractorJS for automated testing of Angular 1.x/2.x applications
[2015/2016] Apache Cordova
JavaFX and HTML5 - Like Curds and Rice
EuroPython 2011 - How to build complex web applications having fun?
Intro To Reactive Programming
Natural Language UI Testing using Behavior Driven Development with Pavlov and...
JVx - Application framework - 2013
02 configuration
Everything you need to know about HTML5 in 15 min
Introducing ASP.NET Core 2.0
Intro to Reactive Programming
Ad

Viewers also liked (20)

PPT
Tracy cv for web 2011
PDF
Social Media Wist-u-dat?
PPSX
SolTec Presentation
PPTX
Samui Luxury Villas - The Estates Samui
PDF
Solutions Simplified - Taking advantage of Cloud
PPTX
Introduction to kanban calgary .net user group - feb 6
PPTX
Twitterとfacebook
PPTX
Visual answers to everyday challenges
PPTX
Unit Testing - Calgary .NET User Group - Nov 26 2014 - Depth Consulting
PPT
Каталог товаров с сайта Racingplanet.de
PPTX
Interface grafica em_java_parte_iii
PPTX
Unlock the hidden value in your membership data presentation overview only
PPTX
Introduction to Kanban
PPTX
Album de fotos
PPT
Rickwebslidedeck 1
PPTX
Interface grafica em_java_parte_ii
PDF
Modulo a
PPTX
Agile Games
PPTX
PPTX
Android App Development for Beginner
Tracy cv for web 2011
Social Media Wist-u-dat?
SolTec Presentation
Samui Luxury Villas - The Estates Samui
Solutions Simplified - Taking advantage of Cloud
Introduction to kanban calgary .net user group - feb 6
Twitterとfacebook
Visual answers to everyday challenges
Unit Testing - Calgary .NET User Group - Nov 26 2014 - Depth Consulting
Каталог товаров с сайта Racingplanet.de
Interface grafica em_java_parte_iii
Unlock the hidden value in your membership data presentation overview only
Introduction to Kanban
Album de fotos
Rickwebslidedeck 1
Interface grafica em_java_parte_ii
Modulo a
Agile Games
Android App Development for Beginner
Ad

Similar to Depth Consulting - Calgary .NET User Group - Apr 22 2015 - Dependency Injection (20)

PDF
From Monoliths to Services: Grafually paying your Technical Debt
PPTX
Integrate Applications into IBM Connections Cloud and On Premises (AD 1632)
PDF
Measure and Increase Developer Productivity with Help of Serverless at AWS Co...
PDF
Patterns and practices for building enterprise-scale HTML5 apps
PPTX
Azure DevOps Tasks.pptx
PPTX
Improving the Design of Existing Software
PPTX
King Tut Architecture
PPTX
Improving the Quality of Existing Software
PDF
From Monoliths to Services: Paying Your Technical Debt
PPTX
Mobile App Architectures & Coding guidelines
PDF
Measure and Increase Developer Productivity with Help of Serverless at JCON 2...
PPTX
How to avoid microservice pitfalls
PDF
Accessibility Testing - Using Asqatasun - Meetup Webinar
PDF
Measure and Increase Developer Productivity with Help of Serverless at Server...
PDF
Droidcon Spain 2016 - The Pragmatic Android Programmer: from hype to reality
PDF
Integrating SharePoint 2010, 2013 and Visual Studio Lightswitch by Rob Windso...
PPTX
O365 Developer Bootcamp NJ 2018 - Material
PPTX
Dev/Test scenarios in DevOps world
PDF
Containers and microservices for realists
PDF
Containers and Microservices for Realists
From Monoliths to Services: Grafually paying your Technical Debt
Integrate Applications into IBM Connections Cloud and On Premises (AD 1632)
Measure and Increase Developer Productivity with Help of Serverless at AWS Co...
Patterns and practices for building enterprise-scale HTML5 apps
Azure DevOps Tasks.pptx
Improving the Design of Existing Software
King Tut Architecture
Improving the Quality of Existing Software
From Monoliths to Services: Paying Your Technical Debt
Mobile App Architectures & Coding guidelines
Measure and Increase Developer Productivity with Help of Serverless at JCON 2...
How to avoid microservice pitfalls
Accessibility Testing - Using Asqatasun - Meetup Webinar
Measure and Increase Developer Productivity with Help of Serverless at Server...
Droidcon Spain 2016 - The Pragmatic Android Programmer: from hype to reality
Integrating SharePoint 2010, 2013 and Visual Studio Lightswitch by Rob Windso...
O365 Developer Bootcamp NJ 2018 - Material
Dev/Test scenarios in DevOps world
Containers and microservices for realists
Containers and Microservices for Realists

Recently uploaded (20)

PDF
System and Network Administraation Chapter 3
PDF
Adobe Illustrator 28.6 Crack My Vision of Vector Design
PPT
Introduction Database Management System for Course Database
PDF
Design an Analysis of Algorithms I-SECS-1021-03
PDF
Upgrade and Innovation Strategies for SAP ERP Customers
PDF
Digital Strategies for Manufacturing Companies
PDF
Nekopoi APK 2025 free lastest update
PDF
How to Migrate SBCGlobal Email to Yahoo Easily
PPTX
Transform Your Business with a Software ERP System
PDF
EN-Survey-Report-SAP-LeanIX-EA-Insights-2025.pdf
PPTX
L1 - Introduction to python Backend.pptx
PDF
Claude Code: Everyone is a 10x Developer - A Comprehensive AI-Powered CLI Tool
PDF
Adobe Premiere Pro 2025 (v24.5.0.057) Crack free
PDF
PTS Company Brochure 2025 (1).pdf.......
PPTX
Agentic AI Use Case- Contract Lifecycle Management (CLM).pptx
PDF
System and Network Administration Chapter 2
PPTX
Agentic AI : A Practical Guide. Undersating, Implementing and Scaling Autono...
PDF
T3DD25 TYPO3 Content Blocks - Deep Dive by André Kraus
PDF
Design an Analysis of Algorithms II-SECS-1021-03
PPTX
Lecture 3: Operating Systems Introduction to Computer Hardware Systems
System and Network Administraation Chapter 3
Adobe Illustrator 28.6 Crack My Vision of Vector Design
Introduction Database Management System for Course Database
Design an Analysis of Algorithms I-SECS-1021-03
Upgrade and Innovation Strategies for SAP ERP Customers
Digital Strategies for Manufacturing Companies
Nekopoi APK 2025 free lastest update
How to Migrate SBCGlobal Email to Yahoo Easily
Transform Your Business with a Software ERP System
EN-Survey-Report-SAP-LeanIX-EA-Insights-2025.pdf
L1 - Introduction to python Backend.pptx
Claude Code: Everyone is a 10x Developer - A Comprehensive AI-Powered CLI Tool
Adobe Premiere Pro 2025 (v24.5.0.057) Crack free
PTS Company Brochure 2025 (1).pdf.......
Agentic AI Use Case- Contract Lifecycle Management (CLM).pptx
System and Network Administration Chapter 2
Agentic AI : A Practical Guide. Undersating, Implementing and Scaling Autono...
T3DD25 TYPO3 Content Blocks - Deep Dive by André Kraus
Design an Analysis of Algorithms II-SECS-1021-03
Lecture 3: Operating Systems Introduction to Computer Hardware Systems

Depth Consulting - Calgary .NET User Group - Apr 22 2015 - Dependency Injection