Cut your Dependencies - Dependency Injection at Silicon Valley Code Camp
- 1. Silicon Valley
Code Camp
Cut your Dependencies
Dependency
Injection
Foothill College, October 6th 2012
- 2. 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
theo@designitright.net
• degree in Software Engineering
and Network Communications www.designitright.net
www.speakerrate.com/theoj
• enjoys cycling, running and eating
www. slideshare.net/theojungeblut
- 3. 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
- 8. Clean Code is maintainable
Source code must be:
• readable & well structured
• extensible
• testable
- 9. 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
- 11. Without Dependency Injection
public class ExampleClass
{
private logger logger;
public ExampleClass()
{
this.logger = new Logger();
this.logger.Log(“Constructor call”);
}
}
- 12. Without Dependency Injection
public class ExampleClass
{
private logger logger;
public ExampleClass()
{
this.logger = new Logger();
this.logger.Log(“Constructor call”);
}
}
- 13. Inversion of Control –
Constructor Injection
http://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”);
}
}
- 14. 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 Code can be developed in parallel.
development
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
- 16. Inversion of Control –
Setter (Property) Injection
http://www.martinfowler.com/articles/injection.html
// UNITY Example
public class ContactManager : IContactManager
{
[Dependency]
public IContactPersistence ContactPersistence
{
get { return this.contactPersistence; }
set { this.contactPersistence = value; }
}
}
- 17. Property Injection
+ Easy to understand
- Hard to implement robust
* Take if an good default exists
- Limited in application otherwise
- 19. 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.
- 20. Ambient Context
• Avoids polluting an API with Cross
Cutting Concerns
• Only for Cross Cutting Concerns
• Limited in application otherwise
- 21. What
are
Dependencies ?
- 22. 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”
- 23. 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”
- 25. “Register, Resolve, Release”
“Three Calls Pattern by Krzysztof Koźmic: http://kozmic.pl/
Build 1. Register Execu Clean
You code Release
up 2. Resolve te up
- 27. Separation of Consern (SoC)
probably by Edsger W. Dijkstra in 1974
• Focus on purpose of your code
• Know only the contracts of the
Execu
You code
dependencies
te
• No need to know
implementations
• No need to handle lifetime of
the dependencies
- 28. The 3 Dimensions of DI
1.Object Composition
2.Object Lifetime
3.Interception
- 29. Register - Composition Root
• XML based Configuration
• Code based Configuration
• Convention based (Discovery)
- 30. Resolve
Resolve a single object request for
example by Consturctor Injection
by resolving the needed object
graph for this object.
- 32. Interception
Public class LoggingInterceptor : IContactManager
{
public bool Save(IContact contact)
{
bool success; Public class ContactManager :
IContactManager
this. logger.Log(“Starting saving’); {
public bool Save(IContact contact)
success = {
this.contactManager.Save(contact); ….
this. logger.Log(“Starting saving’); return Result
}
return success; }
}
}
* Note: strong simplification of what logically happens through interception.
- 35. 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>();
}
}
- 36. 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>();
}
}
- 37. 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
Related Technology:
• Spring.NET • Managed Extensibility Framework (MEF)
• Autofac • Windows Communication Foundation (WCF)
• Puzzle.Nfactory
• Ninject
• PicoContainer.NET
• and more
- 38. The “Must Read”-Book(s)
by Mark Seemann
Dependency
Injection is a set of
software design
principles and
patterns that
enable us to
develop loosely
coupled code.
http://www.manning.com/seemann/
- 39. 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
Graphic by Nathan Sawaya
courtesy of brickartist.com
• 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!)
- 40. Q&A
Downloads,
Feedback & Comments:
theo@designitright.net
www.designitright.net
Graphic by Nathan Sawaya www.speakerrate.com/theoj
courtesy of brickartist.com
www. slideshare.net/theojungeblut
- 42. Please fill out the
feedback, and…
www.speakerrate.com/theoj
… thanks for you attention!
And visit and support the