SlideShare a Scribd company logo
1 of 67
http://slideshare.net/CalebJenkins
“The single greatest thing that you can do to make your
code more testable and healthy is to start taking a
Dependency Injection approach to writing software”
- Real World .NET, C# and Silverlight
Wrox Press 2012
Caleb Jenkins
Super Spy
(saves the world)
Dr. Evil
(bad guy)
public class DoubleOSuperSpy
{
public void StopDrEvilWithSpyWatch()
{
// Stop Dr. Evil
}
}
but what if we wanted to
rescue the queen
use our spy car
stop nuclear attack,
?
?
?
public class DoubleOSuperSpy
{
public void StopDrEvilWithSpyWatch()
{
// Stop Dr. Evil
}
public void StopDrEvilWithSpyCar()
{
}
public void SaveQueenWithSecretCrocodile()
{
}
// Etc...
}
public interface ISecretAgent
{
void RunMission(ISecretMission Mission);
}
public interface ISpyGadget
{
void UseGadget(string Target);
} public interface ISecretMission
{
}
public class SpyWatch : ISpyGadget
public class StopDrEvilMission : ISecretMission
public class DoubleOSuperSpy : ISecretAgent
public class DoubleOSuperSpy : ISecretAgent
{
private ISpyGadget spyWatch;
private IMission stopDrEvil;
public DoubleOSuperSpy()
{
spyWatch = new SpyWatch();
stopDrEvil = new StopDrEvilMission();
}
public void RunMission()
{
spyWatch.UseTo(stopDrEvil);
}
}
Separation of Concerns
+
Grouping of Features
=
Strategy Pattern
public class DoubleOSuperSpy : ISecretAgent
{
private ISpyGadget spyWatch;
private IMission stopDrEvil;
public DoubleOSuperSpy()
{
spyWatch = new SpyWatch();
stopDrEvil = new StopDrEvilMission();
}
public void RunMission()
{
spyWatch.UseTo(stopDrEvil);
}
}
Interfaces
=
Looser Coupling
public class Factory
{
public static ISecretAgent GetSecretAgent()
{
// Check config
return new DoubleOSuperSpy();
}
public static ISecretGadget GetSpyGadget()
{
// Check config
return new SpyWatch();
}
public static ISecretMission GetSecretMission()
{
// Check Config
return new StopDrEvilMission();
}
}
A Factory
Factory Pattern
public class DoubleOSuperSpy : ISecretAgent
{
private ISpyGadget gadget;
private ISecretMission mission;
public DoubleOSuperSpy()
{
gadget = Factory.GetSpyGadget();
mission = Factory.GetSecretMission();
}
public void UseGadget()
{
gadget.UserGadget(mission);
}
}
Abstract Factory Pattern
=
Abstraction of Creation
+
Polymorphism
public class DoubleOSuperSpy : ISecretAgent
{
private ISpyGadget gadget;
private ISecretMission mission;
public DoubleOSuperSpy()
{
gadget = Factory.GetSpyGadget();
mission = Factory.GetSecretMission();
}
public DoubleOSuperSpy (ISecretMission Mission,
ISpyGadget Gadget)
{
mission = Mission;
gadget = Gadget;
}
public ISpyGadget Gadget
{
set { gadget = Value; }
}
public void SetMission(ISecretMission Mission)
{
mission = Mission;
}
public void UseGadget()
{
gadget.UseGadget(mission);
}
public void UseGadget(ISecretMission Mission)
Factory Dependency
Dependency Injection
public class DoubleOSuperSpy : ISecretAgent
{
private ISpyGadget gadget;
private ISecretMission mission;
public DoubleOSuperSpy()
{
gadget = Factory.GetSpyGadget();
mission = Factory.GetSecretMission();
}
public DoubleOSuperSpy (ISecretMission Mission,
ISpyGadget Gadget)
{
mission = Mission;
gadget = Gadget;
}
public ISpyGadet Gadget
{
set { gadget = Value; }
}
public void SetMission(ISecretMission Mission)
{
mission = Mission;
}
public void UseWeapon(ISecretMission Mission)
{
gadget.UserGadget(Mission);
}
public void UseGadget()
public class DoubleOSuperSpy : ISecretAgent
{
private ISpyGadget gadget;
private ISecretMission mission;
public DoubleOSuperSpy()
{
gadget = Factory.GetSpyGadget();
mission = Factory.GetSecretMission();
}
public DoubleOSuperSpy (ISecretMission Mission,
ISpyGadget Gadget)
{
mission = Mission;
gadget = Gadget;
}
public ISpyGadget Gadget
{
set { gadget = Value; }
}
public void SetMission(ISecretMission Mission)
{
mission = Mission;
}
public void RunMission()
{
gadget.UseGadget(mission);
}
public void RunMission (ISecretMission Mission)
{
gadget.UseGadget(Mission);
}
public class DoubleOSuperSpy : ISecretAgent
{
private ISpyGadget gadget;
private ISecretMission mission;
private DoubleOSuperSpy()
{
}
public DoubleOSuperSpy (ISecretMission Mission,
ISpyGadget Gadget)
{
mission = Mission;
gadget = Gadget;
}
public void RunMission()
{
gadget.UseGadget(mission);
}
}
public class MyApplication
{
public void static Main()
{
ISecretAgent agent = Factory.GetSecretAgent();
ISpyGadget gadget = Factory.GetSpyGadget();
ISecretMission mission = Factory.GetSecretMission();
// Manual Dependency Injection
agent.SetMission(mission);
agent.SetGadget(gadget);
agent.RunMissionMission();
}
}
Manual
Dependency Injection
public class MyApplication
{
public void static Main()
{
ISpyGadget gadget = new SpyWatch();
ISecretMission mission = new StopDrEvilMission();
ISecretAgent agent = new DoubleOSuperSpy(gadget);
agent.RunMission(mission);
}
}
Manual
Dependency Injection
Factory
ISpyGadget
ISecretMission
ISecretAgent
MyApplication
Factory
ISpyGadget
ISecretMission
public class MyApplication
{
public void static Main()
{
ISecretAGent agent = Factory.GetSecretAgent();
ISpyGadget gadget = Factory.GetSpyGadget();
ISecretMission mission = Factory.GetSecretMission();
// Manual Dependency Injection
agent.SetMission(mission);
agent.SetSpyGadget(gadget);
agent.RunMission();
}
}
Manual
Dependency Injection
Manual
Dependency Injection
public class MyApplication
{
public void static Main()
{
ISecretAgent agent =
(ISecretAgent) Context.GetInstance(“ISecretAgent”);
}
}
Dependency Injection
Frameworks
Manual
Dependency Injection
public class MyApplication
{
public void static Main()
{
ISecretAgent agent =
(ISecretAgent) Context.GetInstance(“ISecretAgent”);
}
}
Dependency Injection
Frameworks
public class MyApplication
{
public void static Main()
{
ISecretAgent agent =
Context.GetInstance<ISecretAgent>();
}
}
Dependency Injection
(with Generics)
castleproject.org
springframework.net
ninject.org
unity.codeplex.com structuremap.github.io
simpleinjector.org
autofac.org
<factoryconfig type="Improving.ProviderFactory, ProviderFactory">
<factories>
<factory interface=“ISpyGadget" assembly=“Acme.MI6" class=“LaserSpyWatch“ />
<factory interface=“ISecretAgent" assembly=“Acme.MI6" class=“DoubleOSuperSpy”>
<params>
<param name=“Name" value=“Bond, James” type="System.String"/>
</params>
</factory>
<factory interface=“ISecretMission" assembly=“Acme.MI6" class=“StopDrEvilMission“
lifespan=“singleton”>
</factory>
</factories>
</factoryconfig>
Dependency Injection
Configuration Concepts
* Note: This is a conceptual configuration and not specific to any
IoC / di framework. Some IoC’s don’t use config, like Ninject that
relies on special [attributes] for mappings
public class DependencyConfigModule : StandardModule
{
public override void Load()
{
// Factory
Bind<IFactory>().To<factory>().Using<SingletonBehavior>();
// Models
Bind<ISecretAgent>().To<DoubleOSuperSpy>().Using<TransientBehavior>();
Bind<ISecretMission>().To<StopDrEvilMission>().Using<TransientBehavior>();
Bind<ISpyGadget>().To<LaserSpyWatch>().Using<TransientBehavior>();
}
}
Dependency Injection
Configuration Concepts
* Note: This is a conceptual configuration and not specific to any
IoC / di framework. Some IoC’s don’t use config, like Ninject that
relies on special [attributes] for mappings
public class DependencyConfigModule : StandardModule
{
public override void Load()
{
// Factory
Bind<IFactory>().To<factory>().Using<SingletonBehavior>();
// Models
Bind<ISecretAgent>().To<DoubleOSuperSpy>().Using<TransientBehavior>();
Bind<ISecretMission>().To<StopDrEvilMission>().Using<TransientBehavior>();
Bind<ISpyGadget>().To<LaserSpyWatch>().Using<TransientBehavior>();
}
}
<factoryconfig type="Improving.ProviderFactory, ProviderFactory">
<factories>
<factory interface=“ISpyGadget" assembly=“Acme.MI6" class=“LaserSpyWatch“ />
<factory interface=“ISecretAgent" assembly=“Acme.MI6" class=“DoubleOSuperSpy”>
<params>
<param name=“Name" value=“Bond, James” type="System.String"/>
</params>
</factory>
<factory interface=“ISecretMission" assembly=“Acme.MI6" class=“StopDrEvilMission“
lifespan=“singleton”>
</factory>
</factories>
</factoryconfig>
Dependency Injection
Configuration Concepts
Implementation Mapping
Simple Property Injection
Property Injection
Constructor Injection
Instantiation Model:
Singelton
Transient
Pool
* Note: This is a conceptual configuration and not specific to any
IoC / di framework. Some IoC’s don’t use config, like Ninject that
relies on special [attributes] for mappings
Interceptors / Listeners
Per Thread
Generics
LET’S LOOK AT
SOME CODE…
Interceptors and
Listeners
Interceptors and
Listeners
The mission has begun
Dr. Evil has been stopped!
It took :22 seconds!
Interceptors and Listeners
Stop Dr. Evil
Dynamic Proxy
Security must be
licensed to kill
(007)
Logging Bond is
about to begin
mission
Interceptors and Listeners (AOP)
Multi-Threading
Invoke UI Thread
Longest running “complete stack”
Windsor Container
Dynamic Proxy
Active Record (nHibernate)
ASP.NET Mono Rail
Visual Studio Tooling
Well Established Community
Integrates with ASP.NET MVC
ASP.NET | Sharepoint
Winforms | WPF | WCF | WF |
Console Apps
castleproject.org
springframework.net
“Spring Framework” is THE way to
do JAVA development
Spring .NET is the .NET equivalent
Nice bridge for Java Spring
developers moving to .NET
Interface 21
ninject.org
DI “gateway drug”
Light weight / super fast to configure
DI (Integrates with Castle for IoC / AOP)
.NET
Silverlight
Windows Mobile/Phone
No XML Config
(Fluent Config)
unity.codeplex.com
github.com/unitycontainer/unity
From Microsoft
Integration with other Application Blocks
Microsoft Support
Now Maintained by the community (OSS)
castleproject.org
springframework.net
ninject.org
unity.codeplex.com structuremap.github.io
simpleinjector.org
autofac.org
commonservicelocator.codeplex.com
IMPLEMENTATION
Castle Windsor Adapter
Spring .NET Adapter
Unity Adapter
StructureMap Adapter
Autofac Adapter
MEF Adapter now on .NET Framework 4.0
LinFu Adapter
Multi-target CSL binaries
Service Locator Adapter
Implementations
Common Service Locator
commonservicelocator.codeplex.com
Common Service Locator
commonservicelocator.codeplex.com
POOR MAN’S DIpublic class SuperSpyLib
{
private ISecretAgent agent;
private ISpyGadget gadget;
private ISecretMission mission;
public void SuperSpyLib (ISecretAgent Agent,
ISpyGadget Gadget, ISecretMission Mission)
{
agent = Agent;
gadget = Gadget;
mission = Mission;
}
}
POOR MAN’S DIpublic class SuperSpyLib
{
private ISecretAgent agent;
private ISpyGadget gadget;
private ISecretMission mission;
public void SuperSpyLib (ISecretAgent Agent,
ISpyGadget Gadget, ISecretMission Mission)
{
agent = Agent;
gadget = Gadget;
mission = Mission;
}
}
POOR MAN’S DIpublic class SuperSpyLib
{
private ISecretAgent agent;
private ISpyGadget gadget;
private ISecretMission mission;
public void SuperSpyLib (ISecretAgent Agent,
ISpyGadget Gadget, ISecretMission Mission)
{
agent = Agent;
gadget = Gadget;
mission = Mission;
}
}
public class SuperSpyLib
{
private ISecretAgent agent;
private ISpyGadget gadget;
private ISecretMission mission;
public void SuperSpyLib (ISecretAgent Agent,
ISpyGadget Gadget, ISecretMission Mission)
{
agent = Agent;
gadget = Gadget;
mission = Mission;
}
}
public void SuperSpyLib ()
: this (new SuperAgent(),
new SpyAgent(), new StopDrEvilMission())
{
// Default Constructor – Poor Man’s DI
}
POOR MAN’S DIpublic class SuperSpyLib
{
private ISecretAgent agent;
private ISpyGadget gadget;
private ISecretMission mission;
public void SuperSpyLib (ISecretAgent Agent,
ISpyGadget Gadget, ISecretMission Mission)
{
agent = Agent;
gadget = Gadget;
mission = Mission;
}
}
public class SuperSpyLib
{
private ISecretAgent agent;
private ISpyGadget gadget;
private ISecretMission mission;
public void SuperSpyLib (ISecretAgent Agent,
ISpyGadget Gadget, ISecretMission Mission)
{
agent = Agent;
gadget = Gadget;
mission = Mission;
}
}
public void SuperSpyLib ()
: this (new SuperAgent(),
new SpyAgent(), new StopDrEvilMission())
{
// Default Constructor – Poor Man’s DI
}
POOR MAN’S DIpublic class SuperSpyLib
{
private ISecretAgent agent;
private ISpyGadget gadget;
private ISecretMission mission;
public void SuperSpyLib (ISecretAgent Agent,
ISpyGadget Gadget, ISecretMission Mission)
{
agent = Agent;
gadget = Gadget;
mission = Mission;
}
}
public class SuperSpyLib
{
private ISecretAgent agent;
private ISpyGadget gadget;
private ISecretMission mission;
public void SuperSpyLib (ISecretAgent Agent,
ISpyGadget Gadget, ISecretMission Mission)
{
agent = Agent;
gadget = Gadget;
mission = Mission;
}
}
public void SuperSpyLib ()
: this (new SuperAgent(),
new SpyAgent(), new StopDrEvilMission())
{
// Default Constructor – Poor Man’s DI
}
developingUX.com
speakerpedia.com/speakers/caleb-jenkins
@calebjenkins
speakerpedia.com/prog
rammers
developingUX.com
speakerpedia.com/speakers/caleb-jenkins
@calebjenkins

More Related Content

What's hot

Google Plus SignIn : l'Authentification Google
Google Plus SignIn : l'Authentification GoogleGoogle Plus SignIn : l'Authentification Google
Google Plus SignIn : l'Authentification GoogleMathias Seguy
 
Async task, threads, pools, and executors oh my!
Async task, threads, pools, and executors oh my!Async task, threads, pools, and executors oh my!
Async task, threads, pools, and executors oh my!Stacy Devino
 
Android camera2
Android camera2Android camera2
Android camera2Takuma Lee
 
Migrating from Struts 1 to Struts 2
Migrating from Struts 1 to Struts 2Migrating from Struts 1 to Struts 2
Migrating from Struts 1 to Struts 2Matt Raible
 
What is the difference between struts 1 vs struts 2
What is the difference between struts 1 vs struts 2What is the difference between struts 1 vs struts 2
What is the difference between struts 1 vs struts 2Santosh Singh Paliwal
 
Android Jetpack: ViewModel and Testing
Android Jetpack: ViewModel and TestingAndroid Jetpack: ViewModel and Testing
Android Jetpack: ViewModel and TestingYongjun Kim
 
Modern Android Architecture
Modern Android ArchitectureModern Android Architecture
Modern Android ArchitectureEric Maxwell
 
The Ring programming language version 1.5 book - Part 12 of 31
The Ring programming language version 1.5 book - Part 12 of 31The Ring programming language version 1.5 book - Part 12 of 31
The Ring programming language version 1.5 book - Part 12 of 31Mahmoud Samir Fayed
 
Everything You (N)ever Wanted to Know about Testing View Controllers
Everything You (N)ever Wanted to Know about Testing View ControllersEverything You (N)ever Wanted to Know about Testing View Controllers
Everything You (N)ever Wanted to Know about Testing View ControllersBrian Gesiak
 
Alexey Buzdin "Maslow's Pyramid of Android Testing"
Alexey Buzdin "Maslow's Pyramid of Android Testing"Alexey Buzdin "Maslow's Pyramid of Android Testing"
Alexey Buzdin "Maslow's Pyramid of Android Testing"IT Event
 
Androidaop 170105090257
Androidaop 170105090257Androidaop 170105090257
Androidaop 170105090257newegg
 
Androidの本当にあった怖い話
Androidの本当にあった怖い話Androidの本当にあった怖い話
Androidの本当にあった怖い話Yusuke Yamamoto
 
Testing microservices: Tools and Frameworks
Testing microservices: Tools and FrameworksTesting microservices: Tools and Frameworks
Testing microservices: Tools and FrameworksPiotr Mińkowski
 
Effective Android Data Binding
Effective Android Data BindingEffective Android Data Binding
Effective Android Data BindingEric Maxwell
 
RIBs - Fragments which work
RIBs - Fragments which workRIBs - Fragments which work
RIBs - Fragments which workDmitry Zaytsev
 
Quick: Better Tests via Incremental Setup
Quick: Better Tests via Incremental SetupQuick: Better Tests via Incremental Setup
Quick: Better Tests via Incremental SetupBrian Gesiak
 
README.MD for building the first purely digital mobile bank in Indonesia
README.MD for building the first purely digital mobile bank in Indonesia README.MD for building the first purely digital mobile bank in Indonesia
README.MD for building the first purely digital mobile bank in Indonesia Richard Radics
 
Testing view controllers with Quick and Nimble
Testing view controllers with Quick and NimbleTesting view controllers with Quick and Nimble
Testing view controllers with Quick and NimbleMarcio Klepacz
 

What's hot (20)

Google Plus SignIn : l'Authentification Google
Google Plus SignIn : l'Authentification GoogleGoogle Plus SignIn : l'Authentification Google
Google Plus SignIn : l'Authentification Google
 
Async task, threads, pools, and executors oh my!
Async task, threads, pools, and executors oh my!Async task, threads, pools, and executors oh my!
Async task, threads, pools, and executors oh my!
 
Android camera2
Android camera2Android camera2
Android camera2
 
Migrating from Struts 1 to Struts 2
Migrating from Struts 1 to Struts 2Migrating from Struts 1 to Struts 2
Migrating from Struts 1 to Struts 2
 
What is the difference between struts 1 vs struts 2
What is the difference between struts 1 vs struts 2What is the difference between struts 1 vs struts 2
What is the difference between struts 1 vs struts 2
 
Android Jetpack: ViewModel and Testing
Android Jetpack: ViewModel and TestingAndroid Jetpack: ViewModel and Testing
Android Jetpack: ViewModel and Testing
 
Modern Android Architecture
Modern Android ArchitectureModern Android Architecture
Modern Android Architecture
 
The Ring programming language version 1.5 book - Part 12 of 31
The Ring programming language version 1.5 book - Part 12 of 31The Ring programming language version 1.5 book - Part 12 of 31
The Ring programming language version 1.5 book - Part 12 of 31
 
Everything You (N)ever Wanted to Know about Testing View Controllers
Everything You (N)ever Wanted to Know about Testing View ControllersEverything You (N)ever Wanted to Know about Testing View Controllers
Everything You (N)ever Wanted to Know about Testing View Controllers
 
Alexey Buzdin "Maslow's Pyramid of Android Testing"
Alexey Buzdin "Maslow's Pyramid of Android Testing"Alexey Buzdin "Maslow's Pyramid of Android Testing"
Alexey Buzdin "Maslow's Pyramid of Android Testing"
 
Androidaop 170105090257
Androidaop 170105090257Androidaop 170105090257
Androidaop 170105090257
 
Androidの本当にあった怖い話
Androidの本当にあった怖い話Androidの本当にあった怖い話
Androidの本当にあった怖い話
 
Testing microservices: Tools and Frameworks
Testing microservices: Tools and FrameworksTesting microservices: Tools and Frameworks
Testing microservices: Tools and Frameworks
 
Effective Android Data Binding
Effective Android Data BindingEffective Android Data Binding
Effective Android Data Binding
 
RIBs - Fragments which work
RIBs - Fragments which workRIBs - Fragments which work
RIBs - Fragments which work
 
Quick: Better Tests via Incremental Setup
Quick: Better Tests via Incremental SetupQuick: Better Tests via Incremental Setup
Quick: Better Tests via Incremental Setup
 
README.MD for building the first purely digital mobile bank in Indonesia
README.MD for building the first purely digital mobile bank in Indonesia README.MD for building the first purely digital mobile bank in Indonesia
README.MD for building the first purely digital mobile bank in Indonesia
 
Testing view controllers with Quick and Nimble
Testing view controllers with Quick and NimbleTesting view controllers with Quick and Nimble
Testing view controllers with Quick and Nimble
 
Side effects-con-redux
Side effects-con-reduxSide effects-con-redux
Side effects-con-redux
 
Wicket 6
Wicket 6Wicket 6
Wicket 6
 

Similar to Code to DI For - Dependency Injection for Modern Applications

Construire une application JavaFX 8 avec gradle
Construire une application JavaFX 8 avec gradleConstruire une application JavaFX 8 avec gradle
Construire une application JavaFX 8 avec gradleThierry Wasylczenko
 
Secret unit testing tools no one ever told you about
Secret unit testing tools no one ever told you aboutSecret unit testing tools no one ever told you about
Secret unit testing tools no one ever told you aboutDror Helper
 
Android Architecture Components
Android Architecture ComponentsAndroid Architecture Components
Android Architecture ComponentsBurhanuddinRashid
 
Improving android experience for both users and developers
Improving android experience for both users and developersImproving android experience for both users and developers
Improving android experience for both users and developersPavel Lahoda
 
Droidcon2013 android experience lahoda
Droidcon2013 android experience lahodaDroidcon2013 android experience lahoda
Droidcon2013 android experience lahodaDroidcon Berlin
 
Dropwizard and Friends
Dropwizard and FriendsDropwizard and Friends
Dropwizard and FriendsYun Zhi Lin
 
Introduction to Backbone.js & Marionette.js
Introduction to Backbone.js & Marionette.jsIntroduction to Backbone.js & Marionette.js
Introduction to Backbone.js & Marionette.jsReturn on Intelligence
 
Designing for Windows Phone 8
Designing for Windows Phone 8Designing for Windows Phone 8
Designing for Windows Phone 8David Isbitski
 
Parsley & Flex
Parsley & FlexParsley & Flex
Parsley & Flexprideconan
 
Sharper Better Faster Dagger ‡ - Droidcon SF
Sharper Better Faster Dagger ‡ - Droidcon SFSharper Better Faster Dagger ‡ - Droidcon SF
Sharper Better Faster Dagger ‡ - Droidcon SFPierre-Yves Ricau
 
TechDay: Kick Start Your Experience with Android Wear - Mario Viviani
TechDay: Kick Start Your Experience with Android Wear - Mario VivianiTechDay: Kick Start Your Experience with Android Wear - Mario Viviani
TechDay: Kick Start Your Experience with Android Wear - Mario VivianiCodemotion
 
Kick start your experience with android wear - Codemotion Rome 2015
Kick start your experience with android wear - Codemotion Rome 2015Kick start your experience with android wear - Codemotion Rome 2015
Kick start your experience with android wear - Codemotion Rome 2015Codemotion
 
GDG Mediterranean Dev Fest Code lab #DevFestMed15 da android ad android wear ...
GDG Mediterranean Dev Fest Code lab #DevFestMed15 da android ad android wear ...GDG Mediterranean Dev Fest Code lab #DevFestMed15 da android ad android wear ...
GDG Mediterranean Dev Fest Code lab #DevFestMed15 da android ad android wear ...Bruno Salvatore Belluccia
 
Real World Dependency Injection - IPC11 Spring Edition
Real World Dependency Injection - IPC11 Spring EditionReal World Dependency Injection - IPC11 Spring Edition
Real World Dependency Injection - IPC11 Spring EditionStephan Hochdörfer
 
Real World Dependency Injection - oscon13
Real World Dependency Injection - oscon13Real World Dependency Injection - oscon13
Real World Dependency Injection - oscon13Stephan Hochdörfer
 
Android Best Practices
Android Best PracticesAndroid Best Practices
Android Best PracticesYekmer Simsek
 

Similar to Code to DI For - Dependency Injection for Modern Applications (20)

Construire une application JavaFX 8 avec gradle
Construire une application JavaFX 8 avec gradleConstruire une application JavaFX 8 avec gradle
Construire une application JavaFX 8 avec gradle
 
Secret unit testing tools no one ever told you about
Secret unit testing tools no one ever told you aboutSecret unit testing tools no one ever told you about
Secret unit testing tools no one ever told you about
 
Android Architecture Components
Android Architecture ComponentsAndroid Architecture Components
Android Architecture Components
 
Dependency injection in iOS
Dependency injection in iOSDependency injection in iOS
Dependency injection in iOS
 
Android workshop
Android workshopAndroid workshop
Android workshop
 
Improving android experience for both users and developers
Improving android experience for both users and developersImproving android experience for both users and developers
Improving android experience for both users and developers
 
Droidcon2013 android experience lahoda
Droidcon2013 android experience lahodaDroidcon2013 android experience lahoda
Droidcon2013 android experience lahoda
 
Dropwizard and Friends
Dropwizard and FriendsDropwizard and Friends
Dropwizard and Friends
 
Introduction to Backbone.js & Marionette.js
Introduction to Backbone.js & Marionette.jsIntroduction to Backbone.js & Marionette.js
Introduction to Backbone.js & Marionette.js
 
Designing for Windows Phone 8
Designing for Windows Phone 8Designing for Windows Phone 8
Designing for Windows Phone 8
 
Parsley & Flex
Parsley & FlexParsley & Flex
Parsley & Flex
 
Sharper Better Faster Dagger ‡ - Droidcon SF
Sharper Better Faster Dagger ‡ - Droidcon SFSharper Better Faster Dagger ‡ - Droidcon SF
Sharper Better Faster Dagger ‡ - Droidcon SF
 
guice-servlet
guice-servletguice-servlet
guice-servlet
 
TechDay: Kick Start Your Experience with Android Wear - Mario Viviani
TechDay: Kick Start Your Experience with Android Wear - Mario VivianiTechDay: Kick Start Your Experience with Android Wear - Mario Viviani
TechDay: Kick Start Your Experience with Android Wear - Mario Viviani
 
Kick start your experience with android wear - Codemotion Rome 2015
Kick start your experience with android wear - Codemotion Rome 2015Kick start your experience with android wear - Codemotion Rome 2015
Kick start your experience with android wear - Codemotion Rome 2015
 
GDG Mediterranean Dev Fest Code lab #DevFestMed15 da android ad android wear ...
GDG Mediterranean Dev Fest Code lab #DevFestMed15 da android ad android wear ...GDG Mediterranean Dev Fest Code lab #DevFestMed15 da android ad android wear ...
GDG Mediterranean Dev Fest Code lab #DevFestMed15 da android ad android wear ...
 
Real World Dependency Injection - IPC11 Spring Edition
Real World Dependency Injection - IPC11 Spring EditionReal World Dependency Injection - IPC11 Spring Edition
Real World Dependency Injection - IPC11 Spring Edition
 
Real World Dependency Injection - oscon13
Real World Dependency Injection - oscon13Real World Dependency Injection - oscon13
Real World Dependency Injection - oscon13
 
Android Best Practices
Android Best PracticesAndroid Best Practices
Android Best Practices
 
YUI 3
YUI 3YUI 3
YUI 3
 

More from Caleb Jenkins

Get your Hero Groove On - Heroes Reborn
Get your Hero Groove On - Heroes RebornGet your Hero Groove On - Heroes Reborn
Get your Hero Groove On - Heroes RebornCaleb Jenkins
 
Scaling Scrum with UX in the Enterprise
Scaling Scrum with UX in the EnterpriseScaling Scrum with UX in the Enterprise
Scaling Scrum with UX in the EnterpriseCaleb Jenkins
 
Modern Web - MVP Testable WebForms
Modern Web - MVP Testable WebFormsModern Web - MVP Testable WebForms
Modern Web - MVP Testable WebFormsCaleb Jenkins
 
10 Reasons Your Software Sucks 2014 - Tax Day Edition!
10 Reasons Your Software Sucks 2014 - Tax Day Edition!10 Reasons Your Software Sucks 2014 - Tax Day Edition!
10 Reasons Your Software Sucks 2014 - Tax Day Edition!Caleb Jenkins
 
Modern ASP.NET Webskills
Modern ASP.NET WebskillsModern ASP.NET Webskills
Modern ASP.NET WebskillsCaleb Jenkins
 
Prototype Collaborate Innovate
Prototype Collaborate InnovatePrototype Collaborate Innovate
Prototype Collaborate InnovateCaleb Jenkins
 
10 Reasons Your Software Sucks - Election 2012 Edition
10 Reasons Your Software Sucks - Election 2012 Edition10 Reasons Your Software Sucks - Election 2012 Edition
10 Reasons Your Software Sucks - Election 2012 EditionCaleb Jenkins
 
Windows 8 & Phone 8 - an Architectural Battle Plan
Windows 8 & Phone 8 - an Architectural Battle PlanWindows 8 & Phone 8 - an Architectural Battle Plan
Windows 8 & Phone 8 - an Architectural Battle PlanCaleb Jenkins
 
Scaling Scrum with UX
Scaling Scrum with UXScaling Scrum with UX
Scaling Scrum with UXCaleb Jenkins
 
Scaling Scrum with UX
Scaling Scrum with UXScaling Scrum with UX
Scaling Scrum with UXCaleb Jenkins
 
Taming the Monster Legacy Code Beast
Taming the Monster Legacy Code BeastTaming the Monster Legacy Code Beast
Taming the Monster Legacy Code BeastCaleb Jenkins
 
Silverlight for Mobile World Dominations
Silverlight for Mobile World DominationsSilverlight for Mobile World Dominations
Silverlight for Mobile World DominationsCaleb Jenkins
 
.NET on the Cheap - Microsoft + OSS
.NET on the Cheap - Microsoft + OSS.NET on the Cheap - Microsoft + OSS
.NET on the Cheap - Microsoft + OSSCaleb Jenkins
 
10 practices that every developer needs to start right now
10 practices that every developer needs to start right now10 practices that every developer needs to start right now
10 practices that every developer needs to start right nowCaleb Jenkins
 
Threat Modeling - Writing Secure Code
Threat Modeling - Writing Secure CodeThreat Modeling - Writing Secure Code
Threat Modeling - Writing Secure CodeCaleb Jenkins
 
Dependency Injection in Silverlight
Dependency Injection in SilverlightDependency Injection in Silverlight
Dependency Injection in SilverlightCaleb Jenkins
 
Becoming A Presenter in the .NET World
Becoming A Presenter in the .NET WorldBecoming A Presenter in the .NET World
Becoming A Presenter in the .NET WorldCaleb Jenkins
 

More from Caleb Jenkins (20)

Coding Naked 2023
Coding Naked 2023Coding Naked 2023
Coding Naked 2023
 
Development Matters
Development MattersDevelopment Matters
Development Matters
 
Get your Hero Groove On - Heroes Reborn
Get your Hero Groove On - Heroes RebornGet your Hero Groove On - Heroes Reborn
Get your Hero Groove On - Heroes Reborn
 
Scaling Scrum with UX in the Enterprise
Scaling Scrum with UX in the EnterpriseScaling Scrum with UX in the Enterprise
Scaling Scrum with UX in the Enterprise
 
Modern Web - MVP Testable WebForms
Modern Web - MVP Testable WebFormsModern Web - MVP Testable WebForms
Modern Web - MVP Testable WebForms
 
10 Reasons Your Software Sucks 2014 - Tax Day Edition!
10 Reasons Your Software Sucks 2014 - Tax Day Edition!10 Reasons Your Software Sucks 2014 - Tax Day Edition!
10 Reasons Your Software Sucks 2014 - Tax Day Edition!
 
Modern ASP.NET Webskills
Modern ASP.NET WebskillsModern ASP.NET Webskills
Modern ASP.NET Webskills
 
Prototype Collaborate Innovate
Prototype Collaborate InnovatePrototype Collaborate Innovate
Prototype Collaborate Innovate
 
10 Reasons Your Software Sucks - Election 2012 Edition
10 Reasons Your Software Sucks - Election 2012 Edition10 Reasons Your Software Sucks - Election 2012 Edition
10 Reasons Your Software Sucks - Election 2012 Edition
 
Windows 8 & Phone 8 - an Architectural Battle Plan
Windows 8 & Phone 8 - an Architectural Battle PlanWindows 8 & Phone 8 - an Architectural Battle Plan
Windows 8 & Phone 8 - an Architectural Battle Plan
 
Scaling Scrum with UX
Scaling Scrum with UXScaling Scrum with UX
Scaling Scrum with UX
 
Coding Naked
Coding NakedCoding Naked
Coding Naked
 
Scaling Scrum with UX
Scaling Scrum with UXScaling Scrum with UX
Scaling Scrum with UX
 
Taming the Monster Legacy Code Beast
Taming the Monster Legacy Code BeastTaming the Monster Legacy Code Beast
Taming the Monster Legacy Code Beast
 
Silverlight for Mobile World Dominations
Silverlight for Mobile World DominationsSilverlight for Mobile World Dominations
Silverlight for Mobile World Dominations
 
.NET on the Cheap - Microsoft + OSS
.NET on the Cheap - Microsoft + OSS.NET on the Cheap - Microsoft + OSS
.NET on the Cheap - Microsoft + OSS
 
10 practices that every developer needs to start right now
10 practices that every developer needs to start right now10 practices that every developer needs to start right now
10 practices that every developer needs to start right now
 
Threat Modeling - Writing Secure Code
Threat Modeling - Writing Secure CodeThreat Modeling - Writing Secure Code
Threat Modeling - Writing Secure Code
 
Dependency Injection in Silverlight
Dependency Injection in SilverlightDependency Injection in Silverlight
Dependency Injection in Silverlight
 
Becoming A Presenter in the .NET World
Becoming A Presenter in the .NET WorldBecoming A Presenter in the .NET World
Becoming A Presenter in the .NET World
 

Recently uploaded

办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样umasea
 
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdfGOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdfAlina Yurenko
 
Implementing Zero Trust strategy with Azure
Implementing Zero Trust strategy with AzureImplementing Zero Trust strategy with Azure
Implementing Zero Trust strategy with AzureDinusha Kumarasiri
 
React Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief UtamaReact Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief UtamaHanief Utama
 
Unveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML DiagramsUnveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML DiagramsAhmed Mohamed
 
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024StefanoLambiase
 
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)jennyeacort
 
CRM Contender Series: HubSpot vs. Salesforce
CRM Contender Series: HubSpot vs. SalesforceCRM Contender Series: HubSpot vs. Salesforce
CRM Contender Series: HubSpot vs. SalesforceBrainSell Technologies
 
Balasore Best It Company|| Top 10 IT Company || Balasore Software company Odisha
Balasore Best It Company|| Top 10 IT Company || Balasore Software company OdishaBalasore Best It Company|| Top 10 IT Company || Balasore Software company Odisha
Balasore Best It Company|| Top 10 IT Company || Balasore Software company Odishasmiwainfosol
 
Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)OPEN KNOWLEDGE GmbH
 
EY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityEY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityNeo4j
 
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...Christina Lin
 
Folding Cheat Sheet #4 - fourth in a series
Folding Cheat Sheet #4 - fourth in a seriesFolding Cheat Sheet #4 - fourth in a series
Folding Cheat Sheet #4 - fourth in a seriesPhilip Schwarz
 
Buds n Tech IT Solutions: Top-Notch Web Services in Noida
Buds n Tech IT Solutions: Top-Notch Web Services in NoidaBuds n Tech IT Solutions: Top-Notch Web Services in Noida
Buds n Tech IT Solutions: Top-Notch Web Services in Noidabntitsolutionsrishis
 
What are the key points to focus on before starting to learn ETL Development....
What are the key points to focus on before starting to learn ETL Development....What are the key points to focus on before starting to learn ETL Development....
What are the key points to focus on before starting to learn ETL Development....kzayra69
 
What is Advanced Excel and what are some best practices for designing and cre...
What is Advanced Excel and what are some best practices for designing and cre...What is Advanced Excel and what are some best practices for designing and cre...
What is Advanced Excel and what are some best practices for designing and cre...Technogeeks
 
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...soniya singh
 
Cloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEECloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEEVICTOR MAESTRE RAMIREZ
 
SpotFlow: Tracking Method Calls and States at Runtime
SpotFlow: Tracking Method Calls and States at RuntimeSpotFlow: Tracking Method Calls and States at Runtime
SpotFlow: Tracking Method Calls and States at Runtimeandrehoraa
 
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed DataAlluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed DataAlluxio, Inc.
 

Recently uploaded (20)

办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
 
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdfGOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
 
Implementing Zero Trust strategy with Azure
Implementing Zero Trust strategy with AzureImplementing Zero Trust strategy with Azure
Implementing Zero Trust strategy with Azure
 
React Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief UtamaReact Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief Utama
 
Unveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML DiagramsUnveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML Diagrams
 
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
 
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
 
CRM Contender Series: HubSpot vs. Salesforce
CRM Contender Series: HubSpot vs. SalesforceCRM Contender Series: HubSpot vs. Salesforce
CRM Contender Series: HubSpot vs. Salesforce
 
Balasore Best It Company|| Top 10 IT Company || Balasore Software company Odisha
Balasore Best It Company|| Top 10 IT Company || Balasore Software company OdishaBalasore Best It Company|| Top 10 IT Company || Balasore Software company Odisha
Balasore Best It Company|| Top 10 IT Company || Balasore Software company Odisha
 
Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)
 
EY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityEY_Graph Database Powered Sustainability
EY_Graph Database Powered Sustainability
 
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
 
Folding Cheat Sheet #4 - fourth in a series
Folding Cheat Sheet #4 - fourth in a seriesFolding Cheat Sheet #4 - fourth in a series
Folding Cheat Sheet #4 - fourth in a series
 
Buds n Tech IT Solutions: Top-Notch Web Services in Noida
Buds n Tech IT Solutions: Top-Notch Web Services in NoidaBuds n Tech IT Solutions: Top-Notch Web Services in Noida
Buds n Tech IT Solutions: Top-Notch Web Services in Noida
 
What are the key points to focus on before starting to learn ETL Development....
What are the key points to focus on before starting to learn ETL Development....What are the key points to focus on before starting to learn ETL Development....
What are the key points to focus on before starting to learn ETL Development....
 
What is Advanced Excel and what are some best practices for designing and cre...
What is Advanced Excel and what are some best practices for designing and cre...What is Advanced Excel and what are some best practices for designing and cre...
What is Advanced Excel and what are some best practices for designing and cre...
 
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
 
Cloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEECloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEE
 
SpotFlow: Tracking Method Calls and States at Runtime
SpotFlow: Tracking Method Calls and States at RuntimeSpotFlow: Tracking Method Calls and States at Runtime
SpotFlow: Tracking Method Calls and States at Runtime
 
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed DataAlluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
 

Code to DI For - Dependency Injection for Modern Applications

  • 1.
  • 2.
  • 3.
  • 5.
  • 6. “The single greatest thing that you can do to make your code more testable and healthy is to start taking a Dependency Injection approach to writing software” - Real World .NET, C# and Silverlight Wrox Press 2012 Caleb Jenkins
  • 7.
  • 8.
  • 9.
  • 10. Super Spy (saves the world) Dr. Evil (bad guy)
  • 11. public class DoubleOSuperSpy { public void StopDrEvilWithSpyWatch() { // Stop Dr. Evil } }
  • 12. but what if we wanted to rescue the queen use our spy car stop nuclear attack,
  • 13. ?
  • 14. ?
  • 15. ?
  • 16. public class DoubleOSuperSpy { public void StopDrEvilWithSpyWatch() { // Stop Dr. Evil } public void StopDrEvilWithSpyCar() { } public void SaveQueenWithSecretCrocodile() { } // Etc... }
  • 17.
  • 18. public interface ISecretAgent { void RunMission(ISecretMission Mission); } public interface ISpyGadget { void UseGadget(string Target); } public interface ISecretMission { } public class SpyWatch : ISpyGadget public class StopDrEvilMission : ISecretMission public class DoubleOSuperSpy : ISecretAgent
  • 19. public class DoubleOSuperSpy : ISecretAgent { private ISpyGadget spyWatch; private IMission stopDrEvil; public DoubleOSuperSpy() { spyWatch = new SpyWatch(); stopDrEvil = new StopDrEvilMission(); } public void RunMission() { spyWatch.UseTo(stopDrEvil); } } Separation of Concerns + Grouping of Features = Strategy Pattern
  • 20. public class DoubleOSuperSpy : ISecretAgent { private ISpyGadget spyWatch; private IMission stopDrEvil; public DoubleOSuperSpy() { spyWatch = new SpyWatch(); stopDrEvil = new StopDrEvilMission(); } public void RunMission() { spyWatch.UseTo(stopDrEvil); } } Interfaces = Looser Coupling
  • 21. public class Factory { public static ISecretAgent GetSecretAgent() { // Check config return new DoubleOSuperSpy(); } public static ISecretGadget GetSpyGadget() { // Check config return new SpyWatch(); } public static ISecretMission GetSecretMission() { // Check Config return new StopDrEvilMission(); } } A Factory
  • 23. public class DoubleOSuperSpy : ISecretAgent { private ISpyGadget gadget; private ISecretMission mission; public DoubleOSuperSpy() { gadget = Factory.GetSpyGadget(); mission = Factory.GetSecretMission(); } public void UseGadget() { gadget.UserGadget(mission); } } Abstract Factory Pattern = Abstraction of Creation + Polymorphism
  • 24. public class DoubleOSuperSpy : ISecretAgent { private ISpyGadget gadget; private ISecretMission mission; public DoubleOSuperSpy() { gadget = Factory.GetSpyGadget(); mission = Factory.GetSecretMission(); } public DoubleOSuperSpy (ISecretMission Mission, ISpyGadget Gadget) { mission = Mission; gadget = Gadget; } public ISpyGadget Gadget { set { gadget = Value; } } public void SetMission(ISecretMission Mission) { mission = Mission; } public void UseGadget() { gadget.UseGadget(mission); } public void UseGadget(ISecretMission Mission) Factory Dependency Dependency Injection
  • 25. public class DoubleOSuperSpy : ISecretAgent { private ISpyGadget gadget; private ISecretMission mission; public DoubleOSuperSpy() { gadget = Factory.GetSpyGadget(); mission = Factory.GetSecretMission(); } public DoubleOSuperSpy (ISecretMission Mission, ISpyGadget Gadget) { mission = Mission; gadget = Gadget; } public ISpyGadet Gadget { set { gadget = Value; } } public void SetMission(ISecretMission Mission) { mission = Mission; } public void UseWeapon(ISecretMission Mission) { gadget.UserGadget(Mission); } public void UseGadget()
  • 26. public class DoubleOSuperSpy : ISecretAgent { private ISpyGadget gadget; private ISecretMission mission; public DoubleOSuperSpy() { gadget = Factory.GetSpyGadget(); mission = Factory.GetSecretMission(); } public DoubleOSuperSpy (ISecretMission Mission, ISpyGadget Gadget) { mission = Mission; gadget = Gadget; } public ISpyGadget Gadget { set { gadget = Value; } } public void SetMission(ISecretMission Mission) { mission = Mission; } public void RunMission() { gadget.UseGadget(mission); } public void RunMission (ISecretMission Mission) { gadget.UseGadget(Mission); }
  • 27. public class DoubleOSuperSpy : ISecretAgent { private ISpyGadget gadget; private ISecretMission mission; private DoubleOSuperSpy() { } public DoubleOSuperSpy (ISecretMission Mission, ISpyGadget Gadget) { mission = Mission; gadget = Gadget; } public void RunMission() { gadget.UseGadget(mission); } }
  • 28. public class MyApplication { public void static Main() { ISecretAgent agent = Factory.GetSecretAgent(); ISpyGadget gadget = Factory.GetSpyGadget(); ISecretMission mission = Factory.GetSecretMission(); // Manual Dependency Injection agent.SetMission(mission); agent.SetGadget(gadget); agent.RunMissionMission(); } } Manual Dependency Injection
  • 29. public class MyApplication { public void static Main() { ISpyGadget gadget = new SpyWatch(); ISecretMission mission = new StopDrEvilMission(); ISecretAgent agent = new DoubleOSuperSpy(gadget); agent.RunMission(mission); } } Manual Dependency Injection
  • 30.
  • 31.
  • 33. public class MyApplication { public void static Main() { ISecretAGent agent = Factory.GetSecretAgent(); ISpyGadget gadget = Factory.GetSpyGadget(); ISecretMission mission = Factory.GetSecretMission(); // Manual Dependency Injection agent.SetMission(mission); agent.SetSpyGadget(gadget); agent.RunMission(); } } Manual Dependency Injection
  • 34. Manual Dependency Injection public class MyApplication { public void static Main() { ISecretAgent agent = (ISecretAgent) Context.GetInstance(“ISecretAgent”); } } Dependency Injection Frameworks
  • 35. Manual Dependency Injection public class MyApplication { public void static Main() { ISecretAgent agent = (ISecretAgent) Context.GetInstance(“ISecretAgent”); } } Dependency Injection Frameworks public class MyApplication { public void static Main() { ISecretAgent agent = Context.GetInstance<ISecretAgent>(); } } Dependency Injection (with Generics)
  • 37. <factoryconfig type="Improving.ProviderFactory, ProviderFactory"> <factories> <factory interface=“ISpyGadget" assembly=“Acme.MI6" class=“LaserSpyWatch“ /> <factory interface=“ISecretAgent" assembly=“Acme.MI6" class=“DoubleOSuperSpy”> <params> <param name=“Name" value=“Bond, James” type="System.String"/> </params> </factory> <factory interface=“ISecretMission" assembly=“Acme.MI6" class=“StopDrEvilMission“ lifespan=“singleton”> </factory> </factories> </factoryconfig> Dependency Injection Configuration Concepts * Note: This is a conceptual configuration and not specific to any IoC / di framework. Some IoC’s don’t use config, like Ninject that relies on special [attributes] for mappings
  • 38. public class DependencyConfigModule : StandardModule { public override void Load() { // Factory Bind<IFactory>().To<factory>().Using<SingletonBehavior>(); // Models Bind<ISecretAgent>().To<DoubleOSuperSpy>().Using<TransientBehavior>(); Bind<ISecretMission>().To<StopDrEvilMission>().Using<TransientBehavior>(); Bind<ISpyGadget>().To<LaserSpyWatch>().Using<TransientBehavior>(); } } Dependency Injection Configuration Concepts * Note: This is a conceptual configuration and not specific to any IoC / di framework. Some IoC’s don’t use config, like Ninject that relies on special [attributes] for mappings
  • 39. public class DependencyConfigModule : StandardModule { public override void Load() { // Factory Bind<IFactory>().To<factory>().Using<SingletonBehavior>(); // Models Bind<ISecretAgent>().To<DoubleOSuperSpy>().Using<TransientBehavior>(); Bind<ISecretMission>().To<StopDrEvilMission>().Using<TransientBehavior>(); Bind<ISpyGadget>().To<LaserSpyWatch>().Using<TransientBehavior>(); } } <factoryconfig type="Improving.ProviderFactory, ProviderFactory"> <factories> <factory interface=“ISpyGadget" assembly=“Acme.MI6" class=“LaserSpyWatch“ /> <factory interface=“ISecretAgent" assembly=“Acme.MI6" class=“DoubleOSuperSpy”> <params> <param name=“Name" value=“Bond, James” type="System.String"/> </params> </factory> <factory interface=“ISecretMission" assembly=“Acme.MI6" class=“StopDrEvilMission“ lifespan=“singleton”> </factory> </factories> </factoryconfig> Dependency Injection Configuration Concepts Implementation Mapping Simple Property Injection Property Injection Constructor Injection Instantiation Model: Singelton Transient Pool * Note: This is a conceptual configuration and not specific to any IoC / di framework. Some IoC’s don’t use config, like Ninject that relies on special [attributes] for mappings Interceptors / Listeners Per Thread Generics
  • 43. The mission has begun Dr. Evil has been stopped! It took :22 seconds! Interceptors and Listeners
  • 44. Stop Dr. Evil Dynamic Proxy Security must be licensed to kill (007) Logging Bond is about to begin mission Interceptors and Listeners (AOP) Multi-Threading Invoke UI Thread
  • 45. Longest running “complete stack” Windsor Container Dynamic Proxy Active Record (nHibernate) ASP.NET Mono Rail Visual Studio Tooling Well Established Community Integrates with ASP.NET MVC ASP.NET | Sharepoint Winforms | WPF | WCF | WF | Console Apps castleproject.org
  • 46. springframework.net “Spring Framework” is THE way to do JAVA development Spring .NET is the .NET equivalent Nice bridge for Java Spring developers moving to .NET Interface 21
  • 47. ninject.org DI “gateway drug” Light weight / super fast to configure DI (Integrates with Castle for IoC / AOP) .NET Silverlight Windows Mobile/Phone No XML Config (Fluent Config)
  • 48. unity.codeplex.com github.com/unitycontainer/unity From Microsoft Integration with other Application Blocks Microsoft Support Now Maintained by the community (OSS)
  • 50. commonservicelocator.codeplex.com IMPLEMENTATION Castle Windsor Adapter Spring .NET Adapter Unity Adapter StructureMap Adapter Autofac Adapter MEF Adapter now on .NET Framework 4.0 LinFu Adapter Multi-target CSL binaries Service Locator Adapter Implementations
  • 53. POOR MAN’S DIpublic class SuperSpyLib { private ISecretAgent agent; private ISpyGadget gadget; private ISecretMission mission; public void SuperSpyLib (ISecretAgent Agent, ISpyGadget Gadget, ISecretMission Mission) { agent = Agent; gadget = Gadget; mission = Mission; } }
  • 54. POOR MAN’S DIpublic class SuperSpyLib { private ISecretAgent agent; private ISpyGadget gadget; private ISecretMission mission; public void SuperSpyLib (ISecretAgent Agent, ISpyGadget Gadget, ISecretMission Mission) { agent = Agent; gadget = Gadget; mission = Mission; } }
  • 55. POOR MAN’S DIpublic class SuperSpyLib { private ISecretAgent agent; private ISpyGadget gadget; private ISecretMission mission; public void SuperSpyLib (ISecretAgent Agent, ISpyGadget Gadget, ISecretMission Mission) { agent = Agent; gadget = Gadget; mission = Mission; } } public class SuperSpyLib { private ISecretAgent agent; private ISpyGadget gadget; private ISecretMission mission; public void SuperSpyLib (ISecretAgent Agent, ISpyGadget Gadget, ISecretMission Mission) { agent = Agent; gadget = Gadget; mission = Mission; } } public void SuperSpyLib () : this (new SuperAgent(), new SpyAgent(), new StopDrEvilMission()) { // Default Constructor – Poor Man’s DI }
  • 56. POOR MAN’S DIpublic class SuperSpyLib { private ISecretAgent agent; private ISpyGadget gadget; private ISecretMission mission; public void SuperSpyLib (ISecretAgent Agent, ISpyGadget Gadget, ISecretMission Mission) { agent = Agent; gadget = Gadget; mission = Mission; } } public class SuperSpyLib { private ISecretAgent agent; private ISpyGadget gadget; private ISecretMission mission; public void SuperSpyLib (ISecretAgent Agent, ISpyGadget Gadget, ISecretMission Mission) { agent = Agent; gadget = Gadget; mission = Mission; } } public void SuperSpyLib () : this (new SuperAgent(), new SpyAgent(), new StopDrEvilMission()) { // Default Constructor – Poor Man’s DI }
  • 57. POOR MAN’S DIpublic class SuperSpyLib { private ISecretAgent agent; private ISpyGadget gadget; private ISecretMission mission; public void SuperSpyLib (ISecretAgent Agent, ISpyGadget Gadget, ISecretMission Mission) { agent = Agent; gadget = Gadget; mission = Mission; } } public class SuperSpyLib { private ISecretAgent agent; private ISpyGadget gadget; private ISecretMission mission; public void SuperSpyLib (ISecretAgent Agent, ISpyGadget Gadget, ISecretMission Mission) { agent = Agent; gadget = Gadget; mission = Mission; } } public void SuperSpyLib () : this (new SuperAgent(), new SpyAgent(), new StopDrEvilMission()) { // Default Constructor – Poor Man’s DI }
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 66.

Editor's Notes

  1. 59