SlideShare a Scribd company logo
with DI
Mountain View, Sep. 25th, 2013
South Bay.NET
User Group
Cut your
Dependencies
Theo Jungeblut
• Engineering manager & lead by day
at AppDynamics in San Francisco
• Coder & software craftsman by night,
house builder and soon to be dad
• Architects decoupled solutions &
crafts maintainable code to last
• Worked in healthcare and factory
automation, building mission critical
applications, framework & platforms
• Degree in Software Engineering
and Network Communications
• Enjoys cycling, running and eating
theo@designitright.net
www.designitright.net
Rate Session & Win a Shirt
http://www.speakerrate.com/theoj
Where to get the Slides
http://www.slideshare.net/theojungeblut
Overview
• What is the issue?
• What is Dependency Injection?
• What are Dependencies?
• What is the IoC-Container doing for you?
• What, how, why?
• Q & A
UI
Service
ProcessorProcessor
Service
ResourcesResources
UI
Processor
A cleanly layered Architecture
What is the problem?
Cut your Dependencies with - Dependency Injection for South Bay.NET User Group (09/25/2013)
What is Clean Code?
Clean Code is maintainable
Source code must be:
• readable & well structured
• extensible
• testable
Code Maintainability *
Principles Patterns Containers
Why? How? What?
Extensibility Clean Code Tool reuse
* from: Mark Seemann’s “Dependency Injection in .NET” presentation Bay.NET 05/2011
What is
Dependency Injection?
Without Dependency Injection
public class ExampleClass
{
private Logger logger;
public ExampleClass()
{
this.logger = new Logger();
this.logger.Log(“Constructor call”);
}
}
public class ExampleClass
{
private Logger logger;
public ExampleClass()
{
this.logger = new Logger();
this.logger.Log(“Constructor call”);
}
}
Without Dependency Injection
Inversion of Control –
Constructor Injectionhttp://www.martinfowler.com/articles/injection.html
public class ExampleClass
{
private ILogger logger;
public ExampleClass(ILogger logger)
{
this.logger = logger;
if (logger == null)
{
throw new ArgumentNullException(“logger”);
}
this.logger.Log(“Constructor call”);
}
}
Why is Dependency
Injection beneficial?
Benefits of Dependency Injection
Benefit Description
Late binding Services can be swapped with
other services.
Extensibility Code can be extended and reused
in ways not explicitly planned for.
Parallel
development
Code can be developed in parallel.
Maintainability Classes with clearly defined
responsibilities are easier to
maintain.
TESTABILITY Classes can be unit tested.
* from Mark Seemann’s “Dependency Injection in .NET”, page 16
The Adapter Pattern
from Gang of Four, “Design Patterns”
What
are
Dependencies ?
Stable Dependency
“A DEPENDENCY that can be referenced
without any detrimental effects.
The opposite of a VOLATILE DEPENDENCY. “
* From Glossary: Mark Seemann’s “Dependency Injection in .NET”
Volatile Dependency
“A DEPENDENCY that involves side effects that
may be undesirable at times.
This may include modules that don’t yet exist,
or that have adverse requirements on its
runtime environment.
These are the DEPENDENCIES that are
addressed by DI.“
* From Glossary: Mark Seemann’s “Dependency Injection in .NET”
Lifetime
a Job
for the Container
public class ExampleClass
{
private Logger logger;
public ExampleClass()
{
this.logger = new Logger();
this.logger.Log(“Constructor call”);
}
}
Without Dependency Injection
“Register, Resolve, Release”
“Three Calls Pattern by Krzysztof Koźmic: http://kozmic.pl/
1. Register
2. Resolve
Build
up
You code
Execu
te
Release
Clean
up
What the IoC-Container will do for you
1. Register
2. Resolve
Build
up
Release
Clean
up
Separation of Concern (SoC)
probably by Edsger W. Dijkstra in 1974
You code
Execu
te
• Focus on purpose of your code
• Know only the contracts of the
dependencies
• No need to know
implementations
• No need to handle lifetime of
the dependencies
The 3 Dimensions of DI
1.Object Composition
2.Object Lifetime
3.Interception
Register - Composition Root
• XML based Configuration
• Code based Configuration
• Convention based (Discovery)
Resolve
Resolve a single object request for
example by Constructor Injection
by resolving the needed object
graph for this object.
Release
Release objects from Container
when not needed anymore.
Anti
Patterns
Control Freak
http://www.freakingnews.com/The-Puppet-Master-will-play-Pics-102728.asp
// UNITY Example
internal static class Program
{
private static UnityContainer unityContainer;
private static SingleContactManagerForm singleContactManagerForm;
private static void InitializeMainForm()
{
singleContactManagerForm =
unityContainer.Resolve<SingleContactManagerForm>();
}
}
Inversion of Control –
Service Locator
http://www.martinfowler.com/articles/injection.html
// UNITY Example
internal static class Program
{
private static UnityContainer unityContainer;
private static SingleContactManagerForm singleContactManagerForm;
private static void InitializeMainForm()
{
singleContactManagerForm =
unityContainer.Resolve<SingleContactManagerForm>();
}
}
Inversion of Control –
Service Locator
http://www.martinfowler.com/articles/injection.html
Inversion of Control –
Setter (Property) Injection
// UNITY Example
public class ContactManager : IContactManager
{
[Dependency]
public IContactPersistence ContactPersistence
{
get { return this.contactPersistence; }
set { this.contactPersistence = value; }
}
}
http://www.martinfowler.com/articles/injection.html
Property Injection
+ Easy to understand
- Hard to implement robust
* Take if an good default exists
- Limited in application otherwise
Method Injection
public class ContactManager : IContactManager
{
….
public bool Save (IContactPersistencecontactDatabaseService,
IContact contact)
{
if (logger == null)
{
throw new ArgumentNullException(“logger”);
}
…. // Additional business logic executed before calling the save
return contactDatabaseService.Save(contact);
}
}
http://www.martinfowler.com/articles/injection.html
Method Injection
• Needed for handling changing
dependencies in method calls
Ambient Context
public class ContactManager : IContactManager
{
….
public bool Save (….)
{
….
IUser currentUser = ApplicationContext.CurrentUser;
….
}
}
* The Ambient Context object needs to have a default value if not assigned yet.
Ambient Context
• Avoids polluting an API with Cross
Cutting Concerns
• Only for Cross Cutting Concerns
• Limited in application otherwise
Interception
Public class LoggingInterceptor : IContactManager
{
public bool Save(IContact contact)
{
bool success;
this. logger.Log(“Starting saving’);
success =
this.contactManager.Save(contact);
this. logger.Log(“Starting saving’);
return success;
}
}
Public class ContactManager :
IContactManager
{
public bool Save(IContact contact)
{
….
return Result
}
}
* Note: strong simplification of what logically happens through interception.
Dependency Injection Container & more
• Typically support all types of Inversion of Control mechanisms
• Constructor Injection
• Property (Setter) Injection
• Method (Interface) Injection
• Service Locator
•.NET based DI-Container
• Unity
• Castle Windsor
• StructureMap
• Spring.NET
• Autofac
• Puzzle.Nfactory
• Ninject
• PicoContainer.NET
• and more
Related Technology:
• Managed Extensibility Framework (MEF)
The “Must Read”-Book(s)
http://www.manning.com/seemann/
by Mark Seemann
Dependency
Injection is a set of
software design
principles and
patterns that
enable us to
develop loosely
coupled code.
Summary Clean Code - DI
Maintainability is achieved through:
• Simplification, Specialization Decoupling
(KISS, SoC, IoC, DI)
• Dependency Injection
Constructor Injection as default,
Property and Method Injection as needed,
Ambient Context for Dependencies with a default,
Service Locator never
• Registration
Configuration by Convention if possible, exception in Code as needed
Configuration by XML for explicit extensibility and post compile setup
• Quality through Testability
(all of them!)
Graphic by Nathan Sawaya
courtesy of brickartist.com
Downloads,
Feedback & Comments:
Q & A
Graphic by Nathan Sawaya courtesy of brickartist.com
theo@designitright.net
www.designitright.net
www.speakerrate.com/theoj
References…
http://www.manning.com/seemann/
http://en.wikipedia.org/wiki/Keep_it_simple_stupid
http://picocontainer.org/patterns.html
http://en.wikipedia.org/wiki/Dependency_inversion_principle
http://en.wikipedia.org/wiki/Don't_repeat_yourself
http://en.wikipedia.org/wiki/Component-oriented_programming
http://en.wikipedia.org/wiki/Service-oriented_architecture
http://www.martinfowler.com/articles/injection.html
http://www.codeproject.com/KB/aspnet/IOCDI.aspx
http://msdn.microsoft.com/en-us/magazine/cc163739.aspx
http://msdn.microsoft.com/en-us/library/ff650320.aspx
http://msdn.microsoft.com/en-us/library/aa973811.aspx
http://msdn.microsoft.com/en-us/library/ff647976.aspx
http://msdn.microsoft.com/en-us/library/cc707845.aspx
http://msdn.microsoft.com/en-us/library/bb833022.aspx
http://unity.codeplex.com/
Lego (trademarked in capitals as LEGO)
Blog, Rating, Slides
http://www.DesignItRight.net
www.speakerrate.com/theoj
www.slideshare.net/theojungeblut
… thanks for you attention!
And visit and support
Please fill out the
feedback, and…
www.speakerrate.com/theoj

More Related Content

What's hot (20)

PPTX
Clean Code Part III - Craftsmanship at SoCal Code Camp
Theo Jungeblut
 
PDF
Clean Code at Silicon Valley Code Camp 2011 (02/17/2012)
Theo Jungeblut
 
PPTX
Clean Code - Design Patterns and Best Practices for Bay.NET SF User Group (01...
Theo Jungeblut
 
PDF
Clean code
Jean Carlo Machado
 
PPTX
IoC and Mapper in C#
Huy Hoàng Phạm
 
PPTX
Tdd is not about testing (OOP)
Gianluca Padovani
 
PPTX
Tdd is not about testing (C++ version)
Gianluca Padovani
 
PDF
DevSecOps: A Secure SDLC in the Age of DevOps and Hyper-Automation
Alex Senkevitch
 
PDF
Enterprise Java: Just What Is It and the Risks, Threats, and Exposures It Poses
Alex Senkevitch
 
PPTX
Cut your Dependencies with Dependency Injection for East Bay.NET User Group
Theo Jungeblut
 
PPTX
Designing with tests
Dror Helper
 
PDF
Scale14x Patterns and Practices for Open Source Project Success
Stephen Walli
 
PDF
The Open Web
Lachlan Hardy
 
PPTX
Clean Pragmatic Architecture - Avoiding a Monolith
Victor Rentea
 
PPTX
API Documentation -- Presentation to East Bay STC Chapter
Tom Johnson
 
PDF
Modern JavaScript Applications: Design Patterns
Volodymyr Voytyshyn
 
PDF
Tdd is not about testing
Gianluca Padovani
 
PDF
Big Ball of Mud: Software Maintenance Nightmares
Gonzalo Rodríguez
 
PDF
Sandboxing JS and HTML. A lession Learned
Minded Security
 
PDF
My life as a cyborg
Alexander Serebrenik
 
Clean Code Part III - Craftsmanship at SoCal Code Camp
Theo Jungeblut
 
Clean Code at Silicon Valley Code Camp 2011 (02/17/2012)
Theo Jungeblut
 
Clean Code - Design Patterns and Best Practices for Bay.NET SF User Group (01...
Theo Jungeblut
 
Clean code
Jean Carlo Machado
 
IoC and Mapper in C#
Huy Hoàng Phạm
 
Tdd is not about testing (OOP)
Gianluca Padovani
 
Tdd is not about testing (C++ version)
Gianluca Padovani
 
DevSecOps: A Secure SDLC in the Age of DevOps and Hyper-Automation
Alex Senkevitch
 
Enterprise Java: Just What Is It and the Risks, Threats, and Exposures It Poses
Alex Senkevitch
 
Cut your Dependencies with Dependency Injection for East Bay.NET User Group
Theo Jungeblut
 
Designing with tests
Dror Helper
 
Scale14x Patterns and Practices for Open Source Project Success
Stephen Walli
 
The Open Web
Lachlan Hardy
 
Clean Pragmatic Architecture - Avoiding a Monolith
Victor Rentea
 
API Documentation -- Presentation to East Bay STC Chapter
Tom Johnson
 
Modern JavaScript Applications: Design Patterns
Volodymyr Voytyshyn
 
Tdd is not about testing
Gianluca Padovani
 
Big Ball of Mud: Software Maintenance Nightmares
Gonzalo Rodríguez
 
Sandboxing JS and HTML. A lession Learned
Minded Security
 
My life as a cyborg
Alexander Serebrenik
 

Viewers also liked (15)

PPTX
Cut your Dependencies - Dependency Injection at Silicon Valley Code Camp
Theo Jungeblut
 
PDF
7i solutions in short
fho1962
 
PPTX
Cut your Dependencies with Dependency Injection - .NET User Group Osnabrueck
Theo Jungeblut
 
PDF
Chaps 1 2_3 maketing
Mohamed Elmelegy
 
PDF
7i server app-oap-vl2
fho1962
 
PPTX
The simple life of quintina 2
quinv
 
PPTX
Artifact 9 &amp; 10
Tyneil Phillip
 
PPTX
PRSA Pitch_NYU PR MBA Pitch
Nicole Starling
 
PDF
Lego for Software Engineers at Silicon Valley Code Camp 2011 (2010-10-10)
Theo Jungeblut
 
PDF
Lydia's gate newsletter spring 2013
Nycole Kelly
 
DOC
Penitencia
Catequese_Ituiutaba
 
PDF
Contract First Development with Microsoft Code Contracts and Microsoft Pex at...
Theo Jungeblut
 
PPTX
Clean Code III - Software Craftsmanship at SoCal Code Camp San Diego (07/27/2...
Theo Jungeblut
 
PPTX
How to Grow Your Business Faster - Marketing Growth Hacks, Tools, Structure.
David Folwell
 
PDF
Narcotic controlled drugs policy and procedurelast
Knikkos
 
Cut your Dependencies - Dependency Injection at Silicon Valley Code Camp
Theo Jungeblut
 
7i solutions in short
fho1962
 
Cut your Dependencies with Dependency Injection - .NET User Group Osnabrueck
Theo Jungeblut
 
Chaps 1 2_3 maketing
Mohamed Elmelegy
 
7i server app-oap-vl2
fho1962
 
The simple life of quintina 2
quinv
 
Artifact 9 &amp; 10
Tyneil Phillip
 
PRSA Pitch_NYU PR MBA Pitch
Nicole Starling
 
Lego for Software Engineers at Silicon Valley Code Camp 2011 (2010-10-10)
Theo Jungeblut
 
Lydia's gate newsletter spring 2013
Nycole Kelly
 
Contract First Development with Microsoft Code Contracts and Microsoft Pex at...
Theo Jungeblut
 
Clean Code III - Software Craftsmanship at SoCal Code Camp San Diego (07/27/2...
Theo Jungeblut
 
How to Grow Your Business Faster - Marketing Growth Hacks, Tools, Structure.
David Folwell
 
Narcotic controlled drugs policy and procedurelast
Knikkos
 
Ad

Similar to Cut your Dependencies with - Dependency Injection for South Bay.NET User Group (09/25/2013) (20)

PPTX
Clean Code Part II - Dependency Injection at SoCal Code Camp
Theo Jungeblut
 
PPTX
Dependency Injection
Alastair Smith
 
PPTX
Oleksandr Valetskyy - Become a .NET dependency injection ninja with Ninject
Oleksandr Valetskyy
 
PDF
Dependency Injection
Giovanni Scerra ☃
 
PPTX
I gotta dependency on dependency injection
mhenroid
 
PPTX
Design patterns fast track
Binu Bhasuran
 
PDF
dependencyinjectionwithunity.pdf
Jesus330948
 
PPTX
Dependency injection
Ladendirekt OÜ
 
PPTX
Dependency Inversion Principle
Shahriar Hyder
 
PPTX
Depth Consulting - Calgary .NET User Group - Apr 22 2015 - Dependency Injection
Dave White
 
PPTX
Dependency Injection in .NET
ssusere19c741
 
PPTX
Dependency Injection and Autofac
meghantaylor
 
PDF
Dependency injection for beginners
Bhushan Mulmule
 
PPTX
Polaris presentation ioc - code conference
Steven Contos
 
PPTX
Leveraging Dependency Injection(DI) in Universal Applications - Tamir Dresher
Tamir Dresher
 
PPTX
Igor Kochetov "What is wrong with Dependency Injection? Myths and Truths"
Fwdays
 
PDF
Igor Kochetov "What is wrong with Dependency Injection? Myths and Truths"
Fwdays
 
PPTX
Introduction to Dependency Injection
SolTech, Inc.
 
PDF
Dependency Injection, Can We Do Better?
Matt Baker
 
PPTX
Oleksandr Valetskyy - DI vs. IoC
Oleksandr Valetskyy
 
Clean Code Part II - Dependency Injection at SoCal Code Camp
Theo Jungeblut
 
Dependency Injection
Alastair Smith
 
Oleksandr Valetskyy - Become a .NET dependency injection ninja with Ninject
Oleksandr Valetskyy
 
Dependency Injection
Giovanni Scerra ☃
 
I gotta dependency on dependency injection
mhenroid
 
Design patterns fast track
Binu Bhasuran
 
dependencyinjectionwithunity.pdf
Jesus330948
 
Dependency injection
Ladendirekt OÜ
 
Dependency Inversion Principle
Shahriar Hyder
 
Depth Consulting - Calgary .NET User Group - Apr 22 2015 - Dependency Injection
Dave White
 
Dependency Injection in .NET
ssusere19c741
 
Dependency Injection and Autofac
meghantaylor
 
Dependency injection for beginners
Bhushan Mulmule
 
Polaris presentation ioc - code conference
Steven Contos
 
Leveraging Dependency Injection(DI) in Universal Applications - Tamir Dresher
Tamir Dresher
 
Igor Kochetov "What is wrong with Dependency Injection? Myths and Truths"
Fwdays
 
Igor Kochetov "What is wrong with Dependency Injection? Myths and Truths"
Fwdays
 
Introduction to Dependency Injection
SolTech, Inc.
 
Dependency Injection, Can We Do Better?
Matt Baker
 
Oleksandr Valetskyy - DI vs. IoC
Oleksandr Valetskyy
 
Ad

Recently uploaded (20)

PDF
Reverse Engineering of Security Products: Developing an Advanced Microsoft De...
nwbxhhcyjv
 
PDF
"Beyond English: Navigating the Challenges of Building a Ukrainian-language R...
Fwdays
 
PDF
Building Real-Time Digital Twins with IBM Maximo & ArcGIS Indoors
Safe Software
 
PDF
Exolore The Essential AI Tools in 2025.pdf
Srinivasan M
 
PDF
Presentation - Vibe Coding The Future of Tech
yanuarsinggih1
 
PDF
July Patch Tuesday
Ivanti
 
PDF
SWEBOK Guide and Software Services Engineering Education
Hironori Washizaki
 
PPTX
Q2 FY26 Tableau User Group Leader Quarterly Call
lward7
 
PPTX
From Sci-Fi to Reality: Exploring AI Evolution
Svetlana Meissner
 
PDF
Smart Trailers 2025 Update with History and Overview
Paul Menig
 
PDF
Fl Studio 24.2.2 Build 4597 Crack for Windows Free Download 2025
faizk77g
 
PPTX
UiPath Academic Alliance Educator Panels: Session 2 - Business Analyst Content
DianaGray10
 
PDF
Complete JavaScript Notes: From Basics to Advanced Concepts.pdf
haydendavispro
 
PDF
Using FME to Develop Self-Service CAD Applications for a Major UK Police Force
Safe Software
 
PPTX
WooCommerce Workshop: Bring Your Laptop
Laura Hartwig
 
PDF
Timothy Rottach - Ramp up on AI Use Cases, from Vector Search to AI Agents wi...
AWS Chicago
 
PPTX
OpenID AuthZEN - Analyst Briefing July 2025
David Brossard
 
PDF
Chris Elwell Woburn, MA - Passionate About IT Innovation
Chris Elwell Woburn, MA
 
PDF
[Newgen] NewgenONE Marvin Brochure 1.pdf
darshakparmar
 
PDF
HCIP-Data Center Facility Deployment V2.0 Training Material (Without Remarks ...
mcastillo49
 
Reverse Engineering of Security Products: Developing an Advanced Microsoft De...
nwbxhhcyjv
 
"Beyond English: Navigating the Challenges of Building a Ukrainian-language R...
Fwdays
 
Building Real-Time Digital Twins with IBM Maximo & ArcGIS Indoors
Safe Software
 
Exolore The Essential AI Tools in 2025.pdf
Srinivasan M
 
Presentation - Vibe Coding The Future of Tech
yanuarsinggih1
 
July Patch Tuesday
Ivanti
 
SWEBOK Guide and Software Services Engineering Education
Hironori Washizaki
 
Q2 FY26 Tableau User Group Leader Quarterly Call
lward7
 
From Sci-Fi to Reality: Exploring AI Evolution
Svetlana Meissner
 
Smart Trailers 2025 Update with History and Overview
Paul Menig
 
Fl Studio 24.2.2 Build 4597 Crack for Windows Free Download 2025
faizk77g
 
UiPath Academic Alliance Educator Panels: Session 2 - Business Analyst Content
DianaGray10
 
Complete JavaScript Notes: From Basics to Advanced Concepts.pdf
haydendavispro
 
Using FME to Develop Self-Service CAD Applications for a Major UK Police Force
Safe Software
 
WooCommerce Workshop: Bring Your Laptop
Laura Hartwig
 
Timothy Rottach - Ramp up on AI Use Cases, from Vector Search to AI Agents wi...
AWS Chicago
 
OpenID AuthZEN - Analyst Briefing July 2025
David Brossard
 
Chris Elwell Woburn, MA - Passionate About IT Innovation
Chris Elwell Woburn, MA
 
[Newgen] NewgenONE Marvin Brochure 1.pdf
darshakparmar
 
HCIP-Data Center Facility Deployment V2.0 Training Material (Without Remarks ...
mcastillo49
 

Cut your Dependencies with - Dependency Injection for South Bay.NET User Group (09/25/2013)