![Page 1: Cut your Dependencies - Dependency Injection at Silicon Valley Code Camp](https://reader035.vdocuments.us/reader035/viewer/2022062512/553ac91b5503466f348b4602/html5/thumbnails/1.jpg)
Cut your DependenciesDependency
InjectionFoothill College, October 6th 2012
Silicon Valley Code Camp
![Page 2: Cut your Dependencies - Dependency Injection at Silicon Valley Code Camp](https://reader035.vdocuments.us/reader035/viewer/2022062512/553ac91b5503466f348b4602/html5/thumbnails/2.jpg)
Theo Jungeblut• Senior Software Developer at
AppDynamics in San Francisco
• architects decoupled solutions tailored to business needs and crafts maintainable code to last
• worked in healthcare and factory automation, building mission critical applications, framework & platforms for 8+ years
• degree in Software Engineeringand Network Communications
• enjoys cycling, running and eating
www.designitright.net www.speakerrate.com/theoj
www. slideshare.net/theojungeblut
![Page 3: Cut your Dependencies - Dependency Injection at Silicon Valley Code Camp](https://reader035.vdocuments.us/reader035/viewer/2022062512/553ac91b5503466f348b4602/html5/thumbnails/3.jpg)
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
![Page 4: Cut your Dependencies - Dependency Injection at Silicon Valley Code Camp](https://reader035.vdocuments.us/reader035/viewer/2022062512/553ac91b5503466f348b4602/html5/thumbnails/4.jpg)
Cleanly layered Architecture
![Page 5: Cut your Dependencies - Dependency Injection at Silicon Valley Code Camp](https://reader035.vdocuments.us/reader035/viewer/2022062512/553ac91b5503466f348b4602/html5/thumbnails/5.jpg)
What is the problem?
![Page 6: Cut your Dependencies - Dependency Injection at Silicon Valley Code Camp](https://reader035.vdocuments.us/reader035/viewer/2022062512/553ac91b5503466f348b4602/html5/thumbnails/6.jpg)
![Page 7: Cut your Dependencies - Dependency Injection at Silicon Valley Code Camp](https://reader035.vdocuments.us/reader035/viewer/2022062512/553ac91b5503466f348b4602/html5/thumbnails/7.jpg)
What is Clean Code?
![Page 8: Cut your Dependencies - Dependency Injection at Silicon Valley Code Camp](https://reader035.vdocuments.us/reader035/viewer/2022062512/553ac91b5503466f348b4602/html5/thumbnails/8.jpg)
Clean Code is maintainable
Source code must be:• readable & well structured• extensible• testable
![Page 9: Cut your Dependencies - Dependency Injection at Silicon Valley Code Camp](https://reader035.vdocuments.us/reader035/viewer/2022062512/553ac91b5503466f348b4602/html5/thumbnails/9.jpg)
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
![Page 10: Cut your Dependencies - Dependency Injection at Silicon Valley Code Camp](https://reader035.vdocuments.us/reader035/viewer/2022062512/553ac91b5503466f348b4602/html5/thumbnails/10.jpg)
What is Dependency Injection?
![Page 11: Cut your Dependencies - Dependency Injection at Silicon Valley Code Camp](https://reader035.vdocuments.us/reader035/viewer/2022062512/553ac91b5503466f348b4602/html5/thumbnails/11.jpg)
Without Dependency Injection
public class ExampleClass{
private logger logger;
public ExampleClass(){
this.logger = new Logger();
this.logger.Log(“Constructor call”);}
}
![Page 12: Cut your Dependencies - Dependency Injection at Silicon Valley Code Camp](https://reader035.vdocuments.us/reader035/viewer/2022062512/553ac91b5503466f348b4602/html5/thumbnails/12.jpg)
public class ExampleClass{
private logger logger;
public ExampleClass(){
this.logger = new Logger();
this.logger.Log(“Constructor call”);}
}
Without Dependency Injection
Avoid
![Page 13: Cut your Dependencies - Dependency Injection at Silicon Valley Code Camp](https://reader035.vdocuments.us/reader035/viewer/2022062512/553ac91b5503466f348b4602/html5/thumbnails/13.jpg)
Inversion of Control –
Constructor Injectionhttp://www.martinfowler.com/articles/injection.html
public class ExampleClass{
private logger logger;
public ExampleClass(ILogger logger){
this.logger = logger;if (logger == null){
throw new ArgumentNullException(“logger”);
}
this.logger.Log(“Constructor call”);}
}
![Page 14: Cut your Dependencies - Dependency Injection at Silicon Valley Code Camp](https://reader035.vdocuments.us/reader035/viewer/2022062512/553ac91b5503466f348b4602/html5/thumbnails/14.jpg)
Benefits of Dependency InjectionBenefit Description
Late binding Services can be swapped withother services.
Extensibility Code can be extended and reusedin ways not explicitly planned for.
Paralleldevelopment
Code can be developed in parallel.
Maintainability Classes with clearly definedresponsibilities are easier to maintain.
TESTABILITY Classes can be unit tested.
* from Mark Seemann’s “Dependency Injection in .NET”, page 16
![Page 15: Cut your Dependencies - Dependency Injection at Silicon Valley Code Camp](https://reader035.vdocuments.us/reader035/viewer/2022062512/553ac91b5503466f348b4602/html5/thumbnails/15.jpg)
The Adapter Patternfrom Gang of Four, “Design Patterns”
![Page 16: Cut your Dependencies - Dependency Injection at Silicon Valley Code Camp](https://reader035.vdocuments.us/reader035/viewer/2022062512/553ac91b5503466f348b4602/html5/thumbnails/16.jpg)
Inversion of Control –
Setter (Property) Injection
// UNITY Examplepublic class ContactManager : IContactManager{ [Dependency] public IContactPersistence ContactPersistence { get { return this.contactPersistence; }
set { this.contactPersistence = value; } }}
http://www.martinfowler.com/articles/injection.html
![Page 17: Cut your Dependencies - Dependency Injection at Silicon Valley Code Camp](https://reader035.vdocuments.us/reader035/viewer/2022062512/553ac91b5503466f348b4602/html5/thumbnails/17.jpg)
Property Injection
+ Easy to understand
- Hard to implement robust
* Take if an good default exists
- Limited in application otherwise
![Page 18: Cut your Dependencies - Dependency Injection at Silicon Valley Code Camp](https://reader035.vdocuments.us/reader035/viewer/2022062512/553ac91b5503466f348b4602/html5/thumbnails/18.jpg)
Method Injectionpublic 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
![Page 19: Cut your Dependencies - Dependency Injection at Silicon Valley Code Camp](https://reader035.vdocuments.us/reader035/viewer/2022062512/553ac91b5503466f348b4602/html5/thumbnails/19.jpg)
Ambient Contextpublic class ContactManager : IContactManager{ …. public bool Save (….) { …. IUser currentUser = ApplicationContext.CurrentUser; …. }}
* The Ambient Context object needs to have a default value if not assigned yet.
![Page 20: Cut your Dependencies - Dependency Injection at Silicon Valley Code Camp](https://reader035.vdocuments.us/reader035/viewer/2022062512/553ac91b5503466f348b4602/html5/thumbnails/20.jpg)
Ambient Context
• Avoids polluting an API with Cross Cutting Concerns
• Only for Cross Cutting Concerns
• Limited in application otherwise
![Page 21: Cut your Dependencies - Dependency Injection at Silicon Valley Code Camp](https://reader035.vdocuments.us/reader035/viewer/2022062512/553ac91b5503466f348b4602/html5/thumbnails/21.jpg)
What are
Dependencies ?
![Page 22: Cut your Dependencies - Dependency Injection at Silicon Valley Code Camp](https://reader035.vdocuments.us/reader035/viewer/2022062512/553ac91b5503466f348b4602/html5/thumbnails/22.jpg)
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”
![Page 23: Cut your Dependencies - Dependency Injection at Silicon Valley Code Camp](https://reader035.vdocuments.us/reader035/viewer/2022062512/553ac91b5503466f348b4602/html5/thumbnails/23.jpg)
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”
![Page 24: Cut your Dependencies - Dependency Injection at Silicon Valley Code Camp](https://reader035.vdocuments.us/reader035/viewer/2022062512/553ac91b5503466f348b4602/html5/thumbnails/24.jpg)
Lifetime a Job
for the Container
![Page 25: Cut your Dependencies - Dependency Injection at Silicon Valley Code Camp](https://reader035.vdocuments.us/reader035/viewer/2022062512/553ac91b5503466f348b4602/html5/thumbnails/25.jpg)
“Register, Resolve, Release”“Three Calls Pattern by Krzysztof Koźmic: http://kozmic.pl/
1. Register 2. Resolve
Build up You code
Execute ReleaseClean
up
![Page 26: Cut your Dependencies - Dependency Injection at Silicon Valley Code Camp](https://reader035.vdocuments.us/reader035/viewer/2022062512/553ac91b5503466f348b4602/html5/thumbnails/26.jpg)
What the IoC-Container will do for you
1. Register 2. Resolve
Build up
ReleaseClean up
![Page 27: Cut your Dependencies - Dependency Injection at Silicon Valley Code Camp](https://reader035.vdocuments.us/reader035/viewer/2022062512/553ac91b5503466f348b4602/html5/thumbnails/27.jpg)
Separation of Consern (SoC)probably by Edsger W. Dijkstra in 1974
You codeExecu
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
![Page 28: Cut your Dependencies - Dependency Injection at Silicon Valley Code Camp](https://reader035.vdocuments.us/reader035/viewer/2022062512/553ac91b5503466f348b4602/html5/thumbnails/28.jpg)
The 3 Dimensions of DI
1.Object Composition2.Object Lifetime3.Interception
![Page 29: Cut your Dependencies - Dependency Injection at Silicon Valley Code Camp](https://reader035.vdocuments.us/reader035/viewer/2022062512/553ac91b5503466f348b4602/html5/thumbnails/29.jpg)
Register - Composition Root
• XML based Configuration• Code based Configuration• Convention based (Discovery)
![Page 30: Cut your Dependencies - Dependency Injection at Silicon Valley Code Camp](https://reader035.vdocuments.us/reader035/viewer/2022062512/553ac91b5503466f348b4602/html5/thumbnails/30.jpg)
Resolve
Resolve a single object request for example by Consturctor Injection by resolving the needed object graph for this object.
![Page 31: Cut your Dependencies - Dependency Injection at Silicon Valley Code Camp](https://reader035.vdocuments.us/reader035/viewer/2022062512/553ac91b5503466f348b4602/html5/thumbnails/31.jpg)
Release
Release objects from Container when not needed anymore.
![Page 32: Cut your Dependencies - Dependency Injection at Silicon Valley Code Camp](https://reader035.vdocuments.us/reader035/viewer/2022062512/553ac91b5503466f348b4602/html5/thumbnails/32.jpg)
InterceptionPublic 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.
![Page 33: Cut your Dependencies - Dependency Injection at Silicon Valley Code Camp](https://reader035.vdocuments.us/reader035/viewer/2022062512/553ac91b5503466f348b4602/html5/thumbnails/33.jpg)
Anti Patterns
![Page 34: Cut your Dependencies - Dependency Injection at Silicon Valley Code Camp](https://reader035.vdocuments.us/reader035/viewer/2022062512/553ac91b5503466f348b4602/html5/thumbnails/34.jpg)
Control Freak
http://www.freakingnews.com/The-Puppet-Master-will-play-Pics-102728.asp
![Page 35: Cut your Dependencies - Dependency Injection at Silicon Valley Code Camp](https://reader035.vdocuments.us/reader035/viewer/2022062512/553ac91b5503466f348b4602/html5/thumbnails/35.jpg)
// UNITY Exampleinternal static class Program{ private static UnityContainer unityContainer; private static SingleContactManagerForm singleContactManagerForm;
private static void InitializeMainForm() { singleContactManagerForm = unityContainer.Resolve<SingleContactManagerForm>(); }}
Inversion of Control –
Service Locatorhttp://www.martinfowler.com/articles/injection.html
![Page 36: Cut your Dependencies - Dependency Injection at Silicon Valley Code Camp](https://reader035.vdocuments.us/reader035/viewer/2022062512/553ac91b5503466f348b4602/html5/thumbnails/36.jpg)
// UNITY Exampleinternal static class Program{ private static UnityContainer unityContainer; private static SingleContactManagerForm singleContactManagerForm;
private static void InitializeMainForm() { singleContactManagerForm = unityContainer.Resolve<SingleContactManagerForm>(); }}
Inversion of Control –
Service Locatorhttp://www.martinfowler.com/articles/injection.html
Anti-Pattern
based on Mark Seemann
![Page 37: Cut your Dependencies - Dependency Injection at Silicon Valley Code Camp](https://reader035.vdocuments.us/reader035/viewer/2022062512/553ac91b5503466f348b4602/html5/thumbnails/37.jpg)
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)• Windows Communication Foundation (WCF)
![Page 38: Cut your Dependencies - Dependency Injection at Silicon Valley Code Camp](https://reader035.vdocuments.us/reader035/viewer/2022062512/553ac91b5503466f348b4602/html5/thumbnails/38.jpg)
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.
![Page 39: Cut your Dependencies - Dependency Injection at Silicon Valley Code Camp](https://reader035.vdocuments.us/reader035/viewer/2022062512/553ac91b5503466f348b4602/html5/thumbnails/39.jpg)
Summary Clean Code - DIMaintainability is achieved through:
• Simplification, Specialization Decoupling (KISS, SoC, IoC, DI)
• Dependency InjectionConstructor 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 neededConfiguration by XML for explicit extensibility and post compile setup
• Quality through Testability (all of them!)
Graphic by Nathan Sawaya
courtesy of brickartist.com
![Page 40: Cut your Dependencies - Dependency Injection at Silicon Valley Code Camp](https://reader035.vdocuments.us/reader035/viewer/2022062512/553ac91b5503466f348b4602/html5/thumbnails/40.jpg)
Downloads, Feedback & Comments:
Q & A
Graphic by Nathan Sawaya courtesy of brickartist.com
[email protected] www.speakerrate.com/theoj
www. slideshare.net/theojungeblut
![Page 41: Cut your Dependencies - Dependency Injection at Silicon Valley Code Camp](https://reader035.vdocuments.us/reader035/viewer/2022062512/553ac91b5503466f348b4602/html5/thumbnails/41.jpg)
References… http://www.idesign.net/idesign/DesktopDefault.aspx?tabindex=5&tabid=11http://www.manning.com/seemann/http://en.wikipedia.org/wiki/Keep_it_simple_stupidhttp://picocontainer.org/patterns.htmlhttp://en.wikipedia.org/wiki/Dependency_inversion_principlehttp://en.wikipedia.org/wiki/Don't_repeat_yourselfhttp://en.wikipedia.org/wiki/Component-oriented_programminghttp://en.wikipedia.org/wiki/Service-oriented_architecturehttp://www.martinfowler.com/articles/injection.htmlhttp://www.codeproject.com/KB/aspnet/IOCDI.aspxhttp://msdn.microsoft.com/en-us/magazine/cc163739.aspxhttp://msdn.microsoft.com/en-us/library/ff650320.aspxhttp://msdn.microsoft.com/en-us/library/aa973811.aspxhttp://msdn.microsoft.com/en-us/library/ff647976.aspxhttp://msdn.microsoft.com/en-us/library/cc707845.aspxhttp://msdn.microsoft.com/en-us/library/bb833022.aspxhttp://unity.codeplex.com/
Lego (trademarked in capitals as LEGO)
![Page 42: Cut your Dependencies - Dependency Injection at Silicon Valley Code Camp](https://reader035.vdocuments.us/reader035/viewer/2022062512/553ac91b5503466f348b4602/html5/thumbnails/42.jpg)
… thanks for you attention!
And visit and support the
Please fill out the feedback, and…
www.speakerrate.com/theoj