SlideShare a Scribd company logo
1 of 108
Download to read offline
Dependency Injection: 
Why is it awesome 
and why should I care? 
Nolan Erck 
Adobe CF Summit 2014
About Me 
● Consultant (southofshasta.com) 
● Software Development, On-site Training, Design 
● Using ColdFusion since 1999 (4.x) 
● Other stuff: ColdFusion, C++, Java, jQuery, PHP, .NET, 
HTML5, Android, you get the idea... 
● Manager of SacInteractive 
● Yes, I will speak at your UG. 
● Reformed Video Game Developer. 
● Music Junkie
Today's Agenda 
● What is Dependency Injection? 
● When/why do I want it in my projects? 
● Intro to Bean Management. 
● Intro to Aspect Oriented Programming (AOP). 
● Intro to Inversion of Control (IOC). 
● Other info. 
● Questions. 
● Lunch!
Prerequisites 
● Have some experience building classes and 
objects. 
● Don't need to be an expert at it. 
● Don't need to have used an MVC framework. 
● DI frameworks can be totally separate. 
● And 1 more thing you need to know...
Prerequisites 
● Object Oriented Programming is hard. 
● And some of it is confusing at first. 
● And that's NORMAL.
Dependency Injection
What is Dependency Injection? 
● A “design pattern” 
● $6 phrase for “a way to organize your classes so 
they work together in a flexible way”. 
● Sort of like a for() loop, if(), array[]. 
– Reusable technique, based around objects. 
● There are lots of design patterns 
– Dependency Injection is 1 design pattern 
– Inversion of Control (IOC), Aspect Oriented Programming 
(AOP) and Bean Management are all “variations on that 
theme”. 
– There are lots of others too. 
● Book: “Head First Design Patterns”
What is Dependency Injection? 
● It's not platform specific. 
● D/I libraries available for ColdFusion, Java, 
.NET, JavaScript, etc. 
● For today's presentation, I'm using a mix of 
pseudocode and condensed examples from 
various languages. 
● Some of the keywords will change, all the 
concepts work the same across the libraries 
and languages.
Why / When Do I Want 
D/I In My Projects? 
● Short answer: 
When you have lots of classes that talk to each 
other, want to keep code organized, and make it 
easy to swap different “types” of objects in/out of 
your application.
Why / When Do I Want 
D/I In My Projects? 
● Short answer: 
When you have lots of classes that talk to each 
other, want to keep code organized, and make it 
easy to swap different “types” of objects in/out of 
your application. 
● Longer answer: 
That's what the rest of the slides are for...
Does this mean... 
● I have to rebuild my entire app to use D/I? 
● No. But you might get more benefits if you make a 
few changes to legacy code along the way. 
● I have to learn a new way to code all my classes? 
● No. You can leave everything as-is. 
– The nice thing about D/I is you can use it or not 
use it. If you want to write code the “old way” in 
part of your app, there's nothing stopping you 
from doing so. It will all still work as-is. 
● Neat huh? Let's learn a bit more, shall we?
Bean Management
What's a Bean? 
A “thing” your D/I framework knows about. 
class User{ … } 
class DateUtils {…} 
In your DI framework: 
bean id=”myUser” class=”User” 
bean id=”myDateUtils” class=”DateUtils” 
(Exact syntax varies between DI libraries.)
Bean Management 
class A { 
public class A() {...} 
} 
class B{ 
public class B( A a ) 
{ … } 
} 
class C { 
public class C( B b ) 
{ … } 
} 
class D{ 
public class D( C c ) 
{...} 
} 
So if I -JUST- want a D object made...
Bean Management 
I have to do all this... 
A a = new A(); 
B b = new B( a ); 
C c = new C( b ); 
D d = new D( c );
Bean Management 
I have to do all this... 
A a = new A(); 
B b = new B( a ); 
C c = new C( b ); 
D d = new D( c ); 
4 lines of code. The first 3 of which are just “boilerplate” to 
create the variables I need to make a D. 
Multiply that out over all the objects in your app... 
– = a LOT of boilerplate code!
Bean Management 
I have to do all this... 
A a = new A(); 
B b = new B( a ); 
C c = new C( b ); 
D d = new D( c ); 
4 lines of code. The first 3 of which are just “boilerplate” to 
create the variables I need to make a D. 
Multiply that out over all the objects in your app... 
= a LOT of boilerplate code! 
Another way to say it: 
“D depends on A, B and C being made first”.
Bean Management 
With a D/I library that does Bean Management, you'd write 
something like this instead... 
D d = ColdSpring.getBean( “D” );
Bean Management 
With a D/I library that does Bean Management, you'd write 
something like this instead... 
D d = ColdSpring.getBean( “D” ); 
4 lines of code just became 1. And no more boilerplate stuff! 
You tell your D/I framework “I need a D. Please go find out what 
classes D depends on, make those as needed, and hand me 
back the final D when you're done.” 
(“ColdSpring” is kind of a placeholder here for any D/I 
framework. They all work about the same.)
Bean Management 
● Quasi Real World Example 
● Mailing department wide messages. 
● Mail Service CFC 
● Needs a Department CFC to know which 
Department is being emailed 
● Department needs a Department DAO for it's SQL 
stuff. 
● And a SecurityLogin CFC to make sure it's 
validated/logged in/whatever.
Bean Management 
Old Way: 
mySecLogin = CreateObject( “component”, 
“SecurityLogin”); 
myDeptDAO = CreateObject( “component”, 
“DepartmentDAO” ); 
dept = CreateObject( “component”, 
“Department” ); 
dept.init( mySecLogin, myDeptDAO ); 
mailSvc = CreateObject( “component”, “MailService” ); 
mailSvc.init( dept ); 
mailSvc.sendEmail();
Bean Management 
● New Way 
DIFwrk.getBean( “mailService” ).sendMail();
Bean Management 
● New Way 
DIFwrk.getBean( “mailService” ).sendMail(); 
Yes, 1 line of code. 
Yes, it is THAT easy. 
(I'm over simplifying, a LITTLE, for effect.)
Bean Management 
● So how does the D/I framework “know” that I need an A, B and 
C to make a D object? 
● Varies a little from framework to framework. 
● Configuration based: XML file that “describes” the relationship 
between the objects. 
● (ColdSpring, Spring) 
– Nice for, say, white-box testing w/ hands-on QA team. 
● Convention based: 
● Works automatically by which folders you store classes in, 
and how the classes are named. 
● DI/1, WireBox 
● Just pick one and try it out, they all do the same core “things”.
Inversion of Control (IOC)
Inversion of Control (IOC) 
● Fancy term for “flipping” some code around 
from the “traditional” or “normal” way you'd build 
objects. 
● Instead of building an Employee object, then a 
Department object, then putting the Employee 
IN the Department. 
● You say “get me a fully populated/constructed 
Department”. 
● Magic happens, automatically building the 
Employee object for you.
Inversion Of Control (IOC) 
class RockGroup 
{ 
private String groupName; 
public String getGroupName(){ 
return groupName; 
} 
public void setGroupName( String name ) { 
this.groupName = name; 
} 
} 
No Constructor. 
And groupName is 
private. 
So we have to call the 
setter to initialize the 
variable with something.
Inversion Of Control (IOC) 
With regular code, we'd do this: 
String myBandName = “Depeche Mode”; 
RockGroup rg = new RockGroup(); 
rg.setGroupName( myBandName ); 
That works but... 
Every time I want to provide a different default 
value, I have to change code, and recompile.
Inversion Of Control (IOC) 
What if we could provide that default value in a 
“config file”, outside of code? 
And whenever we create a RockGroup object, it 
automatically gets a default value for 
groupName passed in at that instant? 
You can!
Inversion Of Control (IOC) 
Using a D/I framework, you can have a “config” that notes 
default values for your classes: 
<class name=”RockGroup”> 
<property name=”groupName” value=”Depeche Mode” /> 
</class> 
rg = ColdSpring.getBean( “RockGroup” ) 
...any time I make a RockGroup, it will automatically initialize 
the “groupName” variable to the default value I provided. No 
extra lines of code. And changing this default is now done in a 
config file. No recompiling code to change it!
Inversion Of Control (IOC) 
What about classes inside of other classes 
● AKA “Composition” or the “has a” relationship. 
● Kind of the same thing...
Inversion Of Control (IOC) 
class RockGroup{ 
private String groupName; 
public String getGroupName() 
{ … } 
public void 
setGroupName( String name ) 
{…} 
private Musician singer; 
public Musician getSinger() 
{ … } 
public void setSinger( Musician 
m ) {…} 
} 
class Musician{ 
private String name; 
private int yearsSober; 
private int age; 
public String getName() 
{ return name; } 
public void setName( String n ) 
{ this.name = n; } 
public int getYearsSober() { … } 
public void setYearsSober(int y) 
{ … } 
public void setAge( int a ){...} 
public int getAge(){ return age; } 
}
Inversion of Control (IOC) 
With straight code... 
Musician m = new Musician(); 
m.name = “David Lee Roth”; 
m.age = 50; 
m.yearsSober = 10; 
RockGroup rg = new RockGroup(); 
rg.setGroupName( “Van Halen” ); 
rg.setSinger( m );
Inversion of Control (IOC) 
With D/I... 
RockGroup rg = Spring.getBean(“RockGroup”);
Inversion Of Control (IOC) 
The Musician class has a String property, that 
gets initialized just like the String property in 
RockGroup. Ditto for the “int” properties, etc: 
Spring.getBean( “Musician” ); 
– “name” will always be whatever is in my config for the 
Musician class. 
<class name=”Musician”> 
<property name=”name” value=”David Lee Roth” /> 
<property name=”age” value=”50” /> 
<property name=”yearsSober” value=”10” /> 
</class>
Inversion Of Control (IOC) 
You can also refer to other classes in the config, and “inject” them 
into each other, just like we “injected” the String and int into 
Musician: 
<class name=”Singer” id=”mySingerBean”> 
<property name=”name” value=”David Lee Roth” /> 
<property name=”yearsSober” value=”10” /> 
<property name=”age” value=”50” /> 
</class> 
<class name=”RockGroup”> 
<property name=”groupName” value=”Van Halen” /> 
<property name=”singer” ref=”mySingerBean” /> 
</class>
Inversion Of Control (IOC) 
So now when I do this... 
rg = DIFramework.getBean( “RockGroup” ); 
● rg will be fully constructed... 
● It will have a “groupName” property set to “Van 
Halen” 
● It will have a “singer” property set to “David Lee 
Roth”, he'll be 50 years old and 10 years sober. 
● I did that all with 1 line of code, no boilerplate stuff. 
● If I want to change those defaults, I don't have to 
recompile, I just change a “config” setting.
Inversion Of Control (IOC) 
Swapping out one type of Musician for another doesn't require 
recompiling code, just change a config 
<class name=”Singer” id=”singerBean2”> 
<property name=”name” value=”Sammy Hagar” /> 
<property name=”yearsSober” value=”15” /> 
<property name=”age” value=”51” /> 
</class> 
<class name=”RockGroup”> 
<property name=”groupName” value=”Van Halen” /> 
<property name=”singer” ref=”singerBean2” /> 
</class>
Inversion Of Control (IOC) 
Swapping out one type of Musician for another doesn't require 
recompiling code, just change a config 
<class name=”Singer” id=”singerBean2”> 
<property name=”name” value=”Sammy Hagar” /> 
<property name=”yearsSober” value=”15” /> 
<property name=”age” value=”51” /> 
</class> 
<class name=”RockGroup”> 
<property name=”groupName” value=”Van Halen” /> 
<property name=”singer” ref=”singerBean2” /> 
</class> 
Off topic: discussing if Dave is better/worse than Sammy.
Aspect Oriented 
Programming (AOP)
Aspect Oriented Programming (AOP) 
● AKA “Cross Cutting” 
● Change the “aspect” of how a function is called. 
● Say I have a foo() method. 
● Any time foo() is called... 
● Automatically... 
– Call code before or after foo() 
– Wrap some sort of code “around” foo() 
● e.g. try/catch blocks 
● It no longer just does “foo()” 
● Also does whatever you define as an aspect of 
calling foo().
Aspect Oriented Programming (AOP) 
● Example: drawSalesReport() 
● In your AOP library: 
<func name=”drawSalesReport”> 
<aspect before run=”checkIfLoggedIn” /> 
<aspect after run=”saveToDebugLogFile” /> 
</func>
Aspect Oriented Programming (AOP) 
● In your code, it USED to look like this: 
checkIfLoggedIn(); 
drawSalesReport( UserID=555, 
dtCreated='1/1/2015' ); 
SaveToDebugLogFile(); 
What happens NOW is... 
DIFramework.runWithAdvice( “drawSalesReport”, 
{ UserID=555, 
dtCreated='1/1/2015' } ); 
and that does all 3 things, in correct order.
Aspect Oriented Programming (AOP) 
● But wait, there's more! 
● Can also wrap “blocks” of code around each 
other, not just function calls before/after each 
other. 
● e.g. try/catch blocks.
Aspect Oriented Programming (AOP) 
Say I have a query like so: 
<cfquery name=”qryGetUsers”> 
SELECT * FROM Users 
</cfquery> 
In Production, I want that wrapped in a 
try/catch, but in QA/Development, I don't. (So I 
can see the debug output, etc.)
Aspect Oriented Programming (AOP) 
I'd have to set some kind of “flag”: 
<cfif isProduction> 
<cftry> 
<cfquery name=”qryGetUsers”> 
SELECT * FROM Users 
</cfquery> 
<cfcatch> <cflog … /> </cfcatch> 
</cftry> 
<cfelse> 
<cfquery name=”qryGetUsers”> 
SELECT * FROM Users 
</cfquery> 
</cfif> 
Duplicate Code! 
Remember the DRY rule!
Aspect Oriented Programming (AOP) 
● Instead of calling a getUsers() method directly: 
● GetUsers(); 
● In your AOP library: 
<advice around functionName=”getUsersWithAdvice”> 
<method name=”getUsers” /> 
</advice>
Aspect Oriented Programming (AOP) 
<advice around functionName=”getUsersWithAdvice”> 
– <method name=”getUsers” /> 
</advice> 
function getUsersWithAdvice( funcToRun ) { 
if( isProduction ) 
{ 
try{ 
#funcToRun#(); 
} 
catch{ … } 
}else{ 
#funcToRun#(); 
}
Aspect Oriented Programming (AOP) 
In your code: 
DIFramework.runWithAdvice( “getUsers” ); 
● The if() statement is still there but our 
duplicate SQL is gone!
So what's the catch? 
● This is maybe a new way of thinking about how 
you build your classes. 
● Takes some getting used to. 
● A little extra “set up” work 
● Naming conventions, typing some XML 
● Debugging errors can be slower at first. 
...but it does make your code more flexible.
Remember... 
● You don't have to do this all at once. 
● Start with (for example) Bean Management. 
● Add other bits as you get comfortable. 
● It's not like an MVC framework where the 
WHOLE app has to be considered. 
● Find 1 tiny spot in your app, add a little DI there. 
● Add more in increments, go as you learn.
Also Remember... 
● OO Programming is hard. 
● It's different than Procedural code. 
● Takes getting used to. 
● That's NORMAL. 
● Nobody learns all this stuff instantly. 
● It takes some time. 
● (But it is the way most languages are going these 
days.)
Other Resources 
● Book: Spring In Action (Java) 
● Book: Head First Design Patterns 
● Framework/1, DI/1 and AOP/1 
● ColdSpring documentation 
● Good examples, not overwhelming. 
● SpringByExample.org 
● Java code, but explanation of the general concepts 
is pretty good. 
● I'm guessing the ColdBox docs are also great 
for this too. Those guys like to write!
Questions? Comments? 
Ways to reach me... 
nolan@southofshasta.com 
@southofshasta 
www.southofshasta.com 
Thanks!
Dependency Injection: 
Why is it awesome 
and why should I care? 
Nolan Erck 
Adobe CF Summit 2014
About Me 
● Consultant (southofshasta.com) 
● Software Development, On-site Training, Design 
● Using ColdFusion since 1999 (4.x) 
● Other stuff: ColdFusion, C++, Java, jQuery, PHP, .NET, 
HTML5, Android, you get the idea... 
● Manager of SacInteractive 
● Yes, I will speak at your UG. 
● Reformed Video Game Developer. 
● Music Junkie
Today's Agenda 
● What is Dependency Injection? 
● When/why do I want it in my projects? 
● Intro to Bean Management. 
● Intro to Aspect Oriented Programming (AOP). 
● Intro to Inversion of Control (IOC). 
● Other info. 
● Questions. 
● Lunch!
Prerequisites 
● Have some experience building classes and 
objects. 
● Don't need to be an expert at it. 
● Don't need to have used an MVC framework. 
● DI frameworks can be totally separate. 
● And 1 more thing you need to know...
Prerequisites 
● Object Oriented Programming is hard. 
● And some of it is confusing at first. 
● And that's NORMAL.
Dependency Injection
What is Dependency Injection? 
● A “design pattern” 
● $6 phrase for “a way to organize your classes so 
they work together in a flexible way”. 
● Sort of like a for() loop, if(), array[]. 
– Reusable technique, based around objects. 
● There are lots of design patterns 
– Dependency Injection is 1 design pattern 
– Inversion of Control (IOC), Aspect Oriented Programming 
(AOP) and Bean Management are all “variations on that 
theme”. 
– There are lots of others too. 
● Book: “Head First Design Patterns”
What is Dependency Injection? 
● It's not platform specific. 
● D/I libraries available for ColdFusion, Java, 
.NET, JavaScript, etc. 
● For today's presentation, I'm using a mix of 
pseudocode and condensed examples from 
various languages. 
● Some of the keywords will change, all the 
concepts work the same across the libraries 
and languages.
Why / When Do I Want 
D/I In My Projects? 
● Short answer: 
When you have lots of classes that talk to each 
other, want to keep code organized, and make it 
easy to swap different “types” of objects in/out of 
your application.
Why / When Do I Want 
D/I In My Projects? 
● Short answer: 
When you have lots of classes that talk to each 
other, want to keep code organized, and make it 
easy to swap different “types” of objects in/out of 
your application. 
● Longer answer: 
That's what the rest of the slides are for...
Does this mean... 
● I have to rebuild my entire app to use D/I? 
● No. But you might get more benefits if you make a 
few changes to legacy code along the way. 
● I have to learn a new way to code all my classes? 
● No. You can leave everything as-is. 
– The nice thing about D/I is you can use it or not 
use it. If you want to write code the “old way” in 
part of your app, there's nothing stopping you 
from doing so. It will all still work as-is. 
● Neat huh? Let's learn a bit more, shall we?
Bean Management
What's a Bean? 
A “thing” your D/I framework knows about. 
class User{ … } 
class DateUtils {…} 
In your DI framework: 
bean id=”myUser” class=”User” 
bean id=”myDateUtils” class=”DateUtils” 
(Exact syntax varies between DI libraries.)
Bean Management 
class A { 
public class A() {...} 
} 
class B{ 
public class B( A a ) 
{ … } 
} 
class C { 
public class C( B b ) 
{ … } 
} 
class D{ 
public class D( C c ) 
{...} 
} 
So if I -JUST- want a D object made...
Bean Management 
I have to do all this... 
A a = new A(); 
B b = new B( a ); 
C c = new C( b ); 
D d = new D( c );
Bean Management 
I have to do all this... 
A a = new A(); 
B b = new B( a ); 
C c = new C( b ); 
D d = new D( c ); 
4 lines of code. The first 3 of which are just “boilerplate” to 
create the variables I need to make a D. 
Multiply that out over all the objects in your app... 
– = a LOT of boilerplate code!
Bean Management 
I have to do all this... 
A a = new A(); 
B b = new B( a ); 
C c = new C( b ); 
D d = new D( c ); 
4 lines of code. The first 3 of which are just “boilerplate” to 
create the variables I need to make a D. 
Multiply that out over all the objects in your app... 
= a LOT of boilerplate code! 
Another way to say it: 
“D depends on A, B and C being made first”.
Bean Management 
With a D/I library that does Bean Management, you'd write 
something like this instead... 
D d = ColdSpring.getBean( “D” );
Bean Management 
With a D/I library that does Bean Management, you'd write 
something like this instead... 
D d = ColdSpring.getBean( “D” ); 
4 lines of code just became 1. And no more boilerplate stuff! 
You tell your D/I framework “I need a D. Please go find out what 
classes D depends on, make those as needed, and hand me 
back the final D when you're done.” 
(“ColdSpring” is kind of a placeholder here for any D/I 
framework. They all work about the same.)
Bean Management 
● Quasi Real World Example 
● Mailing department wide messages. 
● Mail Service CFC 
● Needs a Department CFC to know which 
Department is being emailed 
● Department needs a Department DAO for it's SQL 
stuff. 
● And a SecurityLogin CFC to make sure it's 
validated/logged in/whatever.
Bean Management 
Old Way: 
mySecLogin = CreateObject( “component”, 
“SecurityLogin”); 
myDeptDAO = CreateObject( “component”, 
“DepartmentDAO” ); 
dept = CreateObject( “component”, 
“Department” ); 
dept.init( mySecLogin, myDeptDAO ); 
mailSvc = CreateObject( “component”, “MailService” ); 
mailSvc.init( dept ); 
mailSvc.sendEmail();
Bean Management 
● New Way 
DIFwrk.getBean( “mailService” ).sendMail();
Bean Management 
● New Way 
DIFwrk.getBean( “mailService” ).sendMail(); 
Yes, 1 line of code. 
Yes, it is THAT easy. 
(I'm over simplifying, a LITTLE, for effect.)
Bean Management 
● So how does the D/I framework “know” that I need an A, B and 
C to make a D object? 
● Varies a little from framework to framework. 
● Configuration based: XML file that “describes” the relationship 
between the objects. 
● (ColdSpring, Spring) 
– Nice for, say, white-box testing w/ hands-on QA team. 
● Convention based: 
● Works automatically by which folders you store classes in, 
and how the classes are named. 
● DI/1, WireBox 
● Just pick one and try it out, they all do the same core “things”.
Inversion of Control (IOC)
Inversion of Control (IOC) 
● Fancy term for “flipping” some code around 
from the “traditional” or “normal” way you'd build 
objects. 
● Instead of building an Employee object, then a 
Department object, then putting the Employee 
IN the Department. 
● You say “get me a fully populated/constructed 
Department”. 
● Magic happens, automatically building the 
Employee object for you.
Inversion Of Control (IOC) 
class RockGroup 
{ 
private String groupName; 
public String getGroupName(){ 
return groupName; 
} 
public void setGroupName( String name ) { 
this.groupName = name; 
} 
} 
No Constructor. 
And groupName is 
private. 
So we have to call the 
setter to initialize the 
variable with something.
Inversion Of Control (IOC) 
With regular code, we'd do this: 
String myBandName = “Depeche Mode”; 
RockGroup rg = new RockGroup(); 
rg.setGroupName( myBandName ); 
That works but... 
Every time I want to provide a different default 
value, I have to change code, and recompile.
Inversion Of Control (IOC) 
What if we could provide that default value in a 
“config file”, outside of code? 
And whenever we create a RockGroup object, it 
automatically gets a default value for 
groupName passed in at that instant? 
You can!
Inversion Of Control (IOC) 
Using a D/I framework, you can have a “config” that notes 
default values for your classes: 
<class name=”RockGroup”> 
<property name=”groupName” value=”Depeche Mode” /> 
</class> 
rg = ColdSpring.getBean( “RockGroup” ) 
...any time I make a RockGroup, it will automatically initialize 
the “groupName” variable to the default value I provided. No 
extra lines of code. And changing this default is now done in a 
config file. No recompiling code to change it!
Inversion Of Control (IOC) 
What about classes inside of other classes 
● AKA “Composition” or the “has a” relationship. 
● Kind of the same thing...
Inversion Of Control (IOC) 
class RockGroup{ 
private String groupName; 
public String getGroupName() 
{ … } 
public void 
setGroupName( String name ) 
{…} 
private Musician singer; 
public Musician getSinger() 
{ … } 
public void setSinger( Musician 
m ) {…} 
} 
class Musician{ 
private String name; 
private int yearsSober; 
private int age; 
public String getName() 
{ return name; } 
public void setName( String n ) 
{ this.name = n; } 
public int getYearsSober() { … } 
public void setYearsSober(int y) 
{ … } 
public void setAge( int a ){...} 
public int getAge(){ return age; } 
}
Inversion of Control (IOC) 
With straight code... 
Musician m = new Musician(); 
m.name = “David Lee Roth”; 
m.age = 50; 
m.yearsSober = 10; 
RockGroup rg = new RockGroup(); 
rg.setGroupName( “Van Halen” ); 
rg.setSinger( m );
Inversion of Control (IOC) 
With D/I... 
RockGroup rg = Spring.getBean(“RockGroup”);
Inversion Of Control (IOC) 
The Musician class has a String property, that 
gets initialized just like the String property in 
RockGroup. Ditto for the “int” properties, etc: 
Spring.getBean( “Musician” ); 
– “name” will always be whatever is in my config for the 
Musician class. 
<class name=”Musician”> 
<property name=”name” value=”David Lee Roth” /> 
<property name=”age” value=”50” /> 
<property name=”yearsSober” value=”10” /> 
</class>
Inversion Of Control (IOC) 
You can also refer to other classes in the config, and “inject” them 
into each other, just like we “injected” the String and int into 
Musician: 
<class name=”Singer” id=”mySingerBean”> 
<property name=”name” value=”David Lee Roth” /> 
<property name=”yearsSober” value=”10” /> 
<property name=”age” value=”50” /> 
</class> 
<class name=”RockGroup”> 
<property name=”groupName” value=”Van Halen” /> 
<property name=”singer” ref=”mySingerBean” /> 
</class>
Inversion Of Control (IOC) 
So now when I do this... 
rg = DIFramework.getBean( “RockGroup” ); 
● rg will be fully constructed... 
● It will have a “groupName” property set to “Van 
Halen” 
● It will have a “singer” property set to “David Lee 
Roth”, he'll be 50 years old and 10 years sober. 
● I did that all with 1 line of code, no boilerplate stuff. 
● If I want to change those defaults, I don't have to 
recompile, I just change a “config” setting.
Inversion Of Control (IOC) 
Swapping out one type of Musician for another doesn't require 
recompiling code, just change a config 
<class name=”Singer” id=”singerBean2”> 
<property name=”name” value=”Sammy Hagar” /> 
<property name=”yearsSober” value=”15” /> 
<property name=”age” value=”51” /> 
</class> 
<class name=”RockGroup”> 
<property name=”groupName” value=”Van Halen” /> 
<property name=”singer” ref=”singerBean2” /> 
</class>
Inversion Of Control (IOC) 
Swapping out one type of Musician for another doesn't require 
recompiling code, just change a config 
<class name=”Singer” id=”singerBean2”> 
<property name=”name” value=”Sammy Hagar” /> 
<property name=”yearsSober” value=”15” /> 
<property name=”age” value=”51” /> 
</class> 
<class name=”RockGroup”> 
<property name=”groupName” value=”Van Halen” /> 
<property name=”singer” ref=”singerBean2” /> 
</class> 
Off topic: discussing if Dave is better/worse than Sammy.
Aspect Oriented 
Programming (AOP)
Aspect Oriented Programming (AOP) 
● AKA “Cross Cutting” 
● Change the “aspect” of how a function is called. 
● Say I have a foo() method. 
● Any time foo() is called... 
● Automatically... 
– Call code before or after foo() 
– Wrap some sort of code “around” foo() 
● e.g. try/catch blocks 
● It no longer just does “foo()” 
● Also does whatever you define as an aspect of 
calling foo().
Aspect Oriented Programming (AOP) 
● Example: drawSalesReport() 
● In your AOP library: 
<func name=”drawSalesReport”> 
<aspect before run=”checkIfLoggedIn” /> 
<aspect after run=”saveToDebugLogFile” /> 
</func>
Aspect Oriented Programming (AOP) 
● In your code, it USED to look like this: 
checkIfLoggedIn(); 
drawSalesReport( UserID=555, 
dtCreated='1/1/2015' ); 
SaveToDebugLogFile(); 
What happens NOW is... 
DIFramework.runWithAdvice( “drawSalesReport”, 
{ UserID=555, 
dtCreated='1/1/2015' } ); 
and that does all 3 things, in correct order.
Aspect Oriented Programming (AOP) 
● But wait, there's more! 
● Can also wrap “blocks” of code around each 
other, not just function calls before/after each 
other. 
● e.g. try/catch blocks.
Aspect Oriented Programming (AOP) 
Say I have a query like so: 
<cfquery name=”qryGetUsers”> 
SELECT * FROM Users 
</cfquery> 
In Production, I want that wrapped in a 
try/catch, but in QA/Development, I don't. (So I 
can see the debug output, etc.)
Aspect Oriented Programming (AOP) 
I'd have to set some kind of “flag”: 
<cfif isProduction> 
<cftry> 
<cfquery name=”qryGetUsers”> 
SELECT * FROM Users 
</cfquery> 
<cfcatch> <cflog … /> </cfcatch> 
</cftry> 
<cfelse> 
<cfquery name=”qryGetUsers”> 
SELECT * FROM Users 
</cfquery> 
</cfif> 
Duplicate Code! 
Remember the DRY rule!
Aspect Oriented Programming (AOP) 
● Instead of calling a getUsers() method directly: 
● GetUsers(); 
● In your AOP library: 
<advice around functionName=”getUsersWithAdvice”> 
<method name=”getUsers” /> 
</advice>
Aspect Oriented Programming (AOP) 
<advice around functionName=”getUsersWithAdvice”> 
– <method name=”getUsers” /> 
</advice> 
function getUsersWithAdvice( funcToRun ) { 
if( isProduction ) 
{ 
try{ 
#funcToRun#(); 
} 
catch{ … } 
}else{ 
#funcToRun#(); 
}
Aspect Oriented Programming (AOP) 
In your code: 
DIFramework.runWithAdvice( “getUsers” ); 
● The if() statement is still there but our 
duplicate SQL is gone!
So what's the catch? 
● This is maybe a new way of thinking about how 
you build your classes. 
● Takes some getting used to. 
● A little extra “set up” work 
● Naming conventions, typing some XML 
● Debugging errors can be slower at first. 
...but it does make your code more flexible.
Remember... 
● You don't have to do this all at once. 
● Start with (for example) Bean Management. 
● Add other bits as you get comfortable. 
● It's not like an MVC framework where the 
WHOLE app has to be considered. 
● Find 1 tiny spot in your app, add a little DI there. 
● Add more in increments, go as you learn.
Also Remember... 
● OO Programming is hard. 
● It's different than Procedural code. 
● Takes getting used to. 
● That's NORMAL. 
● Nobody learns all this stuff instantly. 
● It takes some time. 
● (But it is the way most languages are going these 
days.)
Other Resources 
● Book: Spring In Action (Java) 
● Book: Head First Design Patterns 
● Framework/1, DI/1 and AOP/1 
● ColdSpring documentation 
● Good examples, not overwhelming. 
● SpringByExample.org 
● Java code, but explanation of the general concepts 
is pretty good. 
● I'm guessing the ColdBox docs are also great 
for this too. Those guys like to write!
Questions? Comments? 
Ways to reach me... 
nolan@southofshasta.com 
@southofshasta 
www.southofshasta.com 
Thanks!

More Related Content

What's hot

Understanding Object Oriented Javascript - Coffee@DBG June
Understanding Object Oriented Javascript - Coffee@DBG JuneUnderstanding Object Oriented Javascript - Coffee@DBG June
Understanding Object Oriented Javascript - Coffee@DBG JuneDeepu S Nath
 
Object Oriented JavaScript
Object Oriented JavaScriptObject Oriented JavaScript
Object Oriented JavaScriptMichael Girouard
 
Dart, unicorns and rainbows
Dart, unicorns and rainbowsDart, unicorns and rainbows
Dart, unicorns and rainbowschrisbuckett
 
OOP, API Design and MVP
OOP, API Design and MVPOOP, API Design and MVP
OOP, API Design and MVPHarshith Keni
 
JavaScript - From Birth To Closure
JavaScript - From Birth To ClosureJavaScript - From Birth To Closure
JavaScript - From Birth To ClosureRobert Nyman
 
Dart structured web apps
Dart   structured web appsDart   structured web apps
Dart structured web appschrisbuckett
 
Design patterns illustrated 010PHP
Design patterns illustrated 010PHPDesign patterns illustrated 010PHP
Design patterns illustrated 010PHPHerman Peeren
 
Object Oriented JavaScript
Object Oriented JavaScriptObject Oriented JavaScript
Object Oriented JavaScriptJulie Iskander
 
Design patterns illustrated-2015-03
Design patterns illustrated-2015-03Design patterns illustrated-2015-03
Design patterns illustrated-2015-03Herman Peeren
 
10 Years of JavaScript
10 Years of JavaScript10 Years of JavaScript
10 Years of JavaScriptMike de Boer
 
C# Starter L03-Utilities
C# Starter L03-UtilitiesC# Starter L03-Utilities
C# Starter L03-UtilitiesMohammad Shaker
 
Groovy Update - JavaPolis 2007
Groovy Update - JavaPolis 2007Groovy Update - JavaPolis 2007
Groovy Update - JavaPolis 2007Guillaume Laforge
 
Eclipse Democamp Zurich
Eclipse Democamp ZurichEclipse Democamp Zurich
Eclipse Democamp ZurichMarcel Bruch
 
Metaprogramming With Ruby
Metaprogramming With RubyMetaprogramming With Ruby
Metaprogramming With RubyFarooq Ali
 
JavaScript Programming
JavaScript ProgrammingJavaScript Programming
JavaScript ProgrammingSehwan Noh
 
Oop in-php
Oop in-phpOop in-php
Oop in-phpRajesh S
 

What's hot (20)

Object-oriented Basics
Object-oriented BasicsObject-oriented Basics
Object-oriented Basics
 
Understanding Object Oriented Javascript - Coffee@DBG June
Understanding Object Oriented Javascript - Coffee@DBG JuneUnderstanding Object Oriented Javascript - Coffee@DBG June
Understanding Object Oriented Javascript - Coffee@DBG June
 
Object Oriented JavaScript
Object Oriented JavaScriptObject Oriented JavaScript
Object Oriented JavaScript
 
Js: master prototypes
Js: master prototypesJs: master prototypes
Js: master prototypes
 
Solid OOPS
Solid OOPSSolid OOPS
Solid OOPS
 
Dart, unicorns and rainbows
Dart, unicorns and rainbowsDart, unicorns and rainbows
Dart, unicorns and rainbows
 
OOP, API Design and MVP
OOP, API Design and MVPOOP, API Design and MVP
OOP, API Design and MVP
 
Ch2
Ch2Ch2
Ch2
 
JavaScript - From Birth To Closure
JavaScript - From Birth To ClosureJavaScript - From Birth To Closure
JavaScript - From Birth To Closure
 
Dart structured web apps
Dart   structured web appsDart   structured web apps
Dart structured web apps
 
Design patterns illustrated 010PHP
Design patterns illustrated 010PHPDesign patterns illustrated 010PHP
Design patterns illustrated 010PHP
 
Object Oriented JavaScript
Object Oriented JavaScriptObject Oriented JavaScript
Object Oriented JavaScript
 
Design patterns illustrated-2015-03
Design patterns illustrated-2015-03Design patterns illustrated-2015-03
Design patterns illustrated-2015-03
 
10 Years of JavaScript
10 Years of JavaScript10 Years of JavaScript
10 Years of JavaScript
 
C# Starter L03-Utilities
C# Starter L03-UtilitiesC# Starter L03-Utilities
C# Starter L03-Utilities
 
Groovy Update - JavaPolis 2007
Groovy Update - JavaPolis 2007Groovy Update - JavaPolis 2007
Groovy Update - JavaPolis 2007
 
Eclipse Democamp Zurich
Eclipse Democamp ZurichEclipse Democamp Zurich
Eclipse Democamp Zurich
 
Metaprogramming With Ruby
Metaprogramming With RubyMetaprogramming With Ruby
Metaprogramming With Ruby
 
JavaScript Programming
JavaScript ProgrammingJavaScript Programming
JavaScript Programming
 
Oop in-php
Oop in-phpOop in-php
Oop in-php
 

Similar to Dependency Injection Why is it awesome and Why should I care?

Dependency Injection: Why is awesome and why should I care?
Dependency Injection: Why is awesome and why should I care?Dependency Injection: Why is awesome and why should I care?
Dependency Injection: Why is awesome and why should I care?devObjective
 
HTML5 for the Silverlight Guy
HTML5 for the Silverlight GuyHTML5 for the Silverlight Guy
HTML5 for the Silverlight GuyDavid Padbury
 
Reviewing OOP Design patterns
Reviewing OOP Design patternsReviewing OOP Design patterns
Reviewing OOP Design patternsOlivier Bacs
 
Arquitetura Java em 2007 (Java Architecture in 2007)
Arquitetura Java em 2007 (Java Architecture in 2007)Arquitetura Java em 2007 (Java Architecture in 2007)
Arquitetura Java em 2007 (Java Architecture in 2007)Phil Calçado
 
A gentle intro to the Django Framework
A gentle intro to the Django FrameworkA gentle intro to the Django Framework
A gentle intro to the Django FrameworkRicardo Soares
 
NanoSec Conference 2019: Code Execution Analysis in Mobile Apps - Abdullah Jo...
NanoSec Conference 2019: Code Execution Analysis in Mobile Apps - Abdullah Jo...NanoSec Conference 2019: Code Execution Analysis in Mobile Apps - Abdullah Jo...
NanoSec Conference 2019: Code Execution Analysis in Mobile Apps - Abdullah Jo...Hafez Kamal
 
01 - Intro To Using Java
01 - Intro To Using Java01 - Intro To Using Java
01 - Intro To Using Javathewhiteafrican
 
Performance #5 cpu and battery
Performance #5  cpu and batteryPerformance #5  cpu and battery
Performance #5 cpu and batteryVitali Pekelis
 
Dependency Inversion and Dependency Injection in PHP
Dependency Inversion and Dependency Injection in PHPDependency Inversion and Dependency Injection in PHP
Dependency Inversion and Dependency Injection in PHPmtoppa
 
purely_functional_play_framework_application
purely_functional_play_framework_applicationpurely_functional_play_framework_application
purely_functional_play_framework_applicationNaoki Aoyama
 
AngularJS preso with directives and route resolve
AngularJS preso with directives and route resolveAngularJS preso with directives and route resolve
AngularJS preso with directives and route resolveBrent Goldstein
 
Node.js Patterns for Discerning Developers
Node.js Patterns for Discerning DevelopersNode.js Patterns for Discerning Developers
Node.js Patterns for Discerning Developerscacois
 
JavaOne TS-5098 Groovy SwingBuilder
JavaOne TS-5098 Groovy SwingBuilderJavaOne TS-5098 Groovy SwingBuilder
JavaOne TS-5098 Groovy SwingBuilderAndres Almiray
 
Scaling Rails with Ruby-prof -- Ruby Conf Kenya 2017 by Ben Hughes
Scaling Rails with Ruby-prof -- Ruby Conf Kenya 2017 by Ben Hughes Scaling Rails with Ruby-prof -- Ruby Conf Kenya 2017 by Ben Hughes
Scaling Rails with Ruby-prof -- Ruby Conf Kenya 2017 by Ben Hughes Michael Kimathi
 
Adding a modern twist to legacy web applications
Adding a modern twist to legacy web applicationsAdding a modern twist to legacy web applications
Adding a modern twist to legacy web applicationsJeff Durta
 
Introduction to Software Development
Introduction to Software DevelopmentIntroduction to Software Development
Introduction to Software DevelopmentZeeshan MIrza
 

Similar to Dependency Injection Why is it awesome and Why should I care? (20)

Dependency Injection: Why is awesome and why should I care?
Dependency Injection: Why is awesome and why should I care?Dependency Injection: Why is awesome and why should I care?
Dependency Injection: Why is awesome and why should I care?
 
Dependency Injection
Dependency InjectionDependency Injection
Dependency Injection
 
HTML5 for the Silverlight Guy
HTML5 for the Silverlight GuyHTML5 for the Silverlight Guy
HTML5 for the Silverlight Guy
 
Reviewing OOP Design patterns
Reviewing OOP Design patternsReviewing OOP Design patterns
Reviewing OOP Design patterns
 
Object
ObjectObject
Object
 
Arquitetura Java em 2007 (Java Architecture in 2007)
Arquitetura Java em 2007 (Java Architecture in 2007)Arquitetura Java em 2007 (Java Architecture in 2007)
Arquitetura Java em 2007 (Java Architecture in 2007)
 
A gentle intro to the Django Framework
A gentle intro to the Django FrameworkA gentle intro to the Django Framework
A gentle intro to the Django Framework
 
NanoSec Conference 2019: Code Execution Analysis in Mobile Apps - Abdullah Jo...
NanoSec Conference 2019: Code Execution Analysis in Mobile Apps - Abdullah Jo...NanoSec Conference 2019: Code Execution Analysis in Mobile Apps - Abdullah Jo...
NanoSec Conference 2019: Code Execution Analysis in Mobile Apps - Abdullah Jo...
 
01 - Intro To Using Java
01 - Intro To Using Java01 - Intro To Using Java
01 - Intro To Using Java
 
Performance #5 cpu and battery
Performance #5  cpu and batteryPerformance #5  cpu and battery
Performance #5 cpu and battery
 
Dependency Inversion and Dependency Injection in PHP
Dependency Inversion and Dependency Injection in PHPDependency Inversion and Dependency Injection in PHP
Dependency Inversion and Dependency Injection in PHP
 
purely_functional_play_framework_application
purely_functional_play_framework_applicationpurely_functional_play_framework_application
purely_functional_play_framework_application
 
Intro to OOP
Intro to OOPIntro to OOP
Intro to OOP
 
All of Javascript
All of JavascriptAll of Javascript
All of Javascript
 
AngularJS preso with directives and route resolve
AngularJS preso with directives and route resolveAngularJS preso with directives and route resolve
AngularJS preso with directives and route resolve
 
Node.js Patterns for Discerning Developers
Node.js Patterns for Discerning DevelopersNode.js Patterns for Discerning Developers
Node.js Patterns for Discerning Developers
 
JavaOne TS-5098 Groovy SwingBuilder
JavaOne TS-5098 Groovy SwingBuilderJavaOne TS-5098 Groovy SwingBuilder
JavaOne TS-5098 Groovy SwingBuilder
 
Scaling Rails with Ruby-prof -- Ruby Conf Kenya 2017 by Ben Hughes
Scaling Rails with Ruby-prof -- Ruby Conf Kenya 2017 by Ben Hughes Scaling Rails with Ruby-prof -- Ruby Conf Kenya 2017 by Ben Hughes
Scaling Rails with Ruby-prof -- Ruby Conf Kenya 2017 by Ben Hughes
 
Adding a modern twist to legacy web applications
Adding a modern twist to legacy web applicationsAdding a modern twist to legacy web applications
Adding a modern twist to legacy web applications
 
Introduction to Software Development
Introduction to Software DevelopmentIntroduction to Software Development
Introduction to Software Development
 

More from ColdFusionConference

Building better SQL Server Databases
Building better SQL Server DatabasesBuilding better SQL Server Databases
Building better SQL Server DatabasesColdFusionConference
 
API Economy, Realizing the Business Value of APIs
API Economy, Realizing the Business Value of APIsAPI Economy, Realizing the Business Value of APIs
API Economy, Realizing the Business Value of APIsColdFusionConference
 
Crafting ColdFusion Applications like an Architect
Crafting ColdFusion Applications like an ArchitectCrafting ColdFusion Applications like an Architect
Crafting ColdFusion Applications like an ArchitectColdFusionConference
 
Security And Access Control For APIS using CF API Manager
Security And Access Control For APIS using CF API ManagerSecurity And Access Control For APIS using CF API Manager
Security And Access Control For APIS using CF API ManagerColdFusionConference
 
Monetizing Business Models: ColdFusion and APIS
Monetizing Business Models: ColdFusion and APISMonetizing Business Models: ColdFusion and APIS
Monetizing Business Models: ColdFusion and APISColdFusionConference
 
Become a Security Rockstar with ColdFusion 2016
Become a Security Rockstar with ColdFusion 2016Become a Security Rockstar with ColdFusion 2016
Become a Security Rockstar with ColdFusion 2016ColdFusionConference
 
Developer Insights for Application Upgrade to ColdFusion 2016
Developer Insights for Application Upgrade to ColdFusion 2016Developer Insights for Application Upgrade to ColdFusion 2016
Developer Insights for Application Upgrade to ColdFusion 2016ColdFusionConference
 
ColdFusion Keynote: Building the Agile Web Since 1995
ColdFusion Keynote: Building the Agile Web Since 1995ColdFusion Keynote: Building the Agile Web Since 1995
ColdFusion Keynote: Building the Agile Web Since 1995ColdFusionConference
 
Super Fast Application development with Mura CMS
Super Fast Application development with Mura CMSSuper Fast Application development with Mura CMS
Super Fast Application development with Mura CMSColdFusionConference
 
Build your own secure and real-time dashboard for mobile and web
Build your own secure and real-time dashboard for mobile and webBuild your own secure and real-time dashboard for mobile and web
Build your own secure and real-time dashboard for mobile and webColdFusionConference
 

More from ColdFusionConference (20)

Api manager preconference
Api manager preconferenceApi manager preconference
Api manager preconference
 
Cf ppt vsr
Cf ppt vsrCf ppt vsr
Cf ppt vsr
 
Building better SQL Server Databases
Building better SQL Server DatabasesBuilding better SQL Server Databases
Building better SQL Server Databases
 
API Economy, Realizing the Business Value of APIs
API Economy, Realizing the Business Value of APIsAPI Economy, Realizing the Business Value of APIs
API Economy, Realizing the Business Value of APIs
 
Don't just pdf, Smart PDF
Don't just pdf, Smart PDFDon't just pdf, Smart PDF
Don't just pdf, Smart PDF
 
Crafting ColdFusion Applications like an Architect
Crafting ColdFusion Applications like an ArchitectCrafting ColdFusion Applications like an Architect
Crafting ColdFusion Applications like an Architect
 
Security And Access Control For APIS using CF API Manager
Security And Access Control For APIS using CF API ManagerSecurity And Access Control For APIS using CF API Manager
Security And Access Control For APIS using CF API Manager
 
Monetizing Business Models: ColdFusion and APIS
Monetizing Business Models: ColdFusion and APISMonetizing Business Models: ColdFusion and APIS
Monetizing Business Models: ColdFusion and APIS
 
Become a Security Rockstar with ColdFusion 2016
Become a Security Rockstar with ColdFusion 2016Become a Security Rockstar with ColdFusion 2016
Become a Security Rockstar with ColdFusion 2016
 
ColdFusion in Transit action
ColdFusion in Transit actionColdFusion in Transit action
ColdFusion in Transit action
 
Developer Insights for Application Upgrade to ColdFusion 2016
Developer Insights for Application Upgrade to ColdFusion 2016Developer Insights for Application Upgrade to ColdFusion 2016
Developer Insights for Application Upgrade to ColdFusion 2016
 
Where is cold fusion headed
Where is cold fusion headedWhere is cold fusion headed
Where is cold fusion headed
 
ColdFusion Keynote: Building the Agile Web Since 1995
ColdFusion Keynote: Building the Agile Web Since 1995ColdFusion Keynote: Building the Agile Web Since 1995
ColdFusion Keynote: Building the Agile Web Since 1995
 
Instant ColdFusion with Vagrant
Instant ColdFusion with VagrantInstant ColdFusion with Vagrant
Instant ColdFusion with Vagrant
 
Restful services with ColdFusion
Restful services with ColdFusionRestful services with ColdFusion
Restful services with ColdFusion
 
Super Fast Application development with Mura CMS
Super Fast Application development with Mura CMSSuper Fast Application development with Mura CMS
Super Fast Application development with Mura CMS
 
Build your own secure and real-time dashboard for mobile and web
Build your own secure and real-time dashboard for mobile and webBuild your own secure and real-time dashboard for mobile and web
Build your own secure and real-time dashboard for mobile and web
 
Why Everyone else writes bad code
Why Everyone else writes bad codeWhy Everyone else writes bad code
Why Everyone else writes bad code
 
Securing applications
Securing applicationsSecuring applications
Securing applications
 
Testing automaton
Testing automatonTesting automaton
Testing automaton
 

Recently uploaded

Busty Desi⚡Call Girls in Vasundhara Ghaziabad >༒8448380779 Escort Service
Busty Desi⚡Call Girls in Vasundhara Ghaziabad >༒8448380779 Escort ServiceBusty Desi⚡Call Girls in Vasundhara Ghaziabad >༒8448380779 Escort Service
Busty Desi⚡Call Girls in Vasundhara Ghaziabad >༒8448380779 Escort ServiceDelhi Call girls
 
Busty Desi⚡Call Girls in Surajpur Greater Noida >༒8448380779 Escort Service
Busty Desi⚡Call Girls in Surajpur Greater Noida  >༒8448380779 Escort ServiceBusty Desi⚡Call Girls in Surajpur Greater Noida  >༒8448380779 Escort Service
Busty Desi⚡Call Girls in Surajpur Greater Noida >༒8448380779 Escort ServiceDelhi Call girls
 
Busty Desi⚡Call Girls in Vasundhara Ghaziabad >༒8448380779 Escort Service
Busty Desi⚡Call Girls in Vasundhara Ghaziabad >༒8448380779 Escort ServiceBusty Desi⚡Call Girls in Vasundhara Ghaziabad >༒8448380779 Escort Service
Busty Desi⚡Call Girls in Vasundhara Ghaziabad >༒8448380779 Escort ServiceDelhi Call girls
 
GENUINE Babe,Call Girls IN Dwarka Delhi | +91-8377087607
GENUINE Babe,Call Girls IN Dwarka Delhi | +91-8377087607GENUINE Babe,Call Girls IN Dwarka Delhi | +91-8377087607
GENUINE Babe,Call Girls IN Dwarka Delhi | +91-8377087607dollysharma2066
 
Enjoy Night ≽ 8448380779 ≼ Call Girls In Gurgaon Sector 54 (Gurgaon)
Enjoy Night ≽ 8448380779 ≼ Call Girls In Gurgaon Sector 54 (Gurgaon)Enjoy Night ≽ 8448380779 ≼ Call Girls In Gurgaon Sector 54 (Gurgaon)
Enjoy Night ≽ 8448380779 ≼ Call Girls In Gurgaon Sector 54 (Gurgaon)Delhi Call girls
 
Enjoy Night⚡Call Girls Mayur Vihar Delhi >༒8448380779 Escort Service
Enjoy Night⚡Call Girls Mayur Vihar Delhi >༒8448380779 Escort ServiceEnjoy Night⚡Call Girls Mayur Vihar Delhi >༒8448380779 Escort Service
Enjoy Night⚡Call Girls Mayur Vihar Delhi >༒8448380779 Escort ServiceDelhi Call girls
 
Enjoy Night ≽ 8448380779 ≼ Call Girls In Gurgaon Sector 57 (Gurgaon)
Enjoy Night ≽ 8448380779 ≼ Call Girls In Gurgaon Sector 57 (Gurgaon)Enjoy Night ≽ 8448380779 ≼ Call Girls In Gurgaon Sector 57 (Gurgaon)
Enjoy Night ≽ 8448380779 ≼ Call Girls In Gurgaon Sector 57 (Gurgaon)Delhi Call girls
 
Sector 4, Noida Call girls :8448380779 Model Escorts | 100% verified
Sector 4, Noida Call girls :8448380779 Model Escorts | 100% verifiedSector 4, Noida Call girls :8448380779 Model Escorts | 100% verified
Sector 4, Noida Call girls :8448380779 Model Escorts | 100% verifiedDelhi Call girls
 
Busty Desi⚡Call Girls in Pari Chowk Escorts, Noida >༒8448380779 Escort Service
Busty Desi⚡Call Girls in Pari Chowk Escorts, Noida >༒8448380779 Escort ServiceBusty Desi⚡Call Girls in Pari Chowk Escorts, Noida >༒8448380779 Escort Service
Busty Desi⚡Call Girls in Pari Chowk Escorts, Noida >༒8448380779 Escort ServiceDelhi Call girls
 
Busty Desi⚡Call Girls in Surajpur Greater Noida >༒8448380779 Escort Service
Busty Desi⚡Call Girls in Surajpur Greater Noida >༒8448380779 Escort ServiceBusty Desi⚡Call Girls in Surajpur Greater Noida >༒8448380779 Escort Service
Busty Desi⚡Call Girls in Surajpur Greater Noida >༒8448380779 Escort ServiceDelhi Call girls
 
Enjoy Night ≽ 8448380779 ≼ Call Girls In Gurgaon Sector 53 (Gurgaon)
Enjoy Night ≽ 8448380779 ≼ Call Girls In Gurgaon Sector 53 (Gurgaon)Enjoy Night ≽ 8448380779 ≼ Call Girls In Gurgaon Sector 53 (Gurgaon)
Enjoy Night ≽ 8448380779 ≼ Call Girls In Gurgaon Sector 53 (Gurgaon)Delhi Call girls
 
Busty Desi⚡Call Girls in Pari Chowk Escorts, Noida >༒8448380779 Escort Service
Busty Desi⚡Call Girls in Pari Chowk Escorts, Noida >༒8448380779 Escort ServiceBusty Desi⚡Call Girls in Pari Chowk Escorts, Noida >༒8448380779 Escort Service
Busty Desi⚡Call Girls in Pari Chowk Escorts, Noida >༒8448380779 Escort ServiceDelhi Call girls
 
call girls in Indirapuram (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service...
call girls in Indirapuram (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service...call girls in Indirapuram (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service...
call girls in Indirapuram (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service...Delhi Call girls
 
Busty Desi⚡Call Girls in Pari Chowk Escorts, Noida >༒8448380779 Escort Service
Busty Desi⚡Call Girls in Pari Chowk Escorts, Noida >༒8448380779 Escort ServiceBusty Desi⚡Call Girls in Pari Chowk Escorts, Noida >༒8448380779 Escort Service
Busty Desi⚡Call Girls in Pari Chowk Escorts, Noida >༒8448380779 Escort ServiceDelhi Call girls
 
Enjoy Night ≽ 8448380779 ≼ Call Girls In Gurgaon Sector 56 (Gurgaon)
Enjoy Night ≽ 8448380779 ≼ Call Girls In Gurgaon Sector 56 (Gurgaon)Enjoy Night ≽ 8448380779 ≼ Call Girls In Gurgaon Sector 56 (Gurgaon)
Enjoy Night ≽ 8448380779 ≼ Call Girls In Gurgaon Sector 56 (Gurgaon)Delhi Call girls
 
Sector 10, Noida Call girls :8448380779 Model Escorts | 100% verified
Sector 10, Noida Call girls :8448380779 Model Escorts | 100% verifiedSector 10, Noida Call girls :8448380779 Model Escorts | 100% verified
Sector 10, Noida Call girls :8448380779 Model Escorts | 100% verifiedDelhi Call girls
 
Enjoy Night ≽ 8448380779 ≼ Call Girls In Gurgaon Sector 66 (Gurgaon)
Enjoy Night ≽ 8448380779 ≼ Call Girls In Gurgaon Sector 66 (Gurgaon)Enjoy Night ≽ 8448380779 ≼ Call Girls In Gurgaon Sector 66 (Gurgaon)
Enjoy Night ≽ 8448380779 ≼ Call Girls In Gurgaon Sector 66 (Gurgaon)Delhi Call girls
 
Enjoy Night ≽ 8448380779 ≼ Call Girls In Gurgaon Sector 52 (Gurgaon)
Enjoy Night ≽ 8448380779 ≼ Call Girls In Gurgaon Sector 52 (Gurgaon)Enjoy Night ≽ 8448380779 ≼ Call Girls In Gurgaon Sector 52 (Gurgaon)
Enjoy Night ≽ 8448380779 ≼ Call Girls In Gurgaon Sector 52 (Gurgaon)Delhi Call girls
 
Dubai Call Girls Centerfold O525547819 Call Girls Dubai Cfnm
Dubai Call Girls Centerfold O525547819 Call Girls Dubai CfnmDubai Call Girls Centerfold O525547819 Call Girls Dubai Cfnm
Dubai Call Girls Centerfold O525547819 Call Girls Dubai Cfnmkojalkojal131
 
Call Girls In Vasant Kunj ☎✔ 9871031762 ☎✔ Russion Escorts Service In Delhi Ncr
Call Girls In Vasant Kunj ☎✔ 9871031762 ☎✔ Russion Escorts Service In Delhi NcrCall Girls In Vasant Kunj ☎✔ 9871031762 ☎✔ Russion Escorts Service In Delhi Ncr
Call Girls In Vasant Kunj ☎✔ 9871031762 ☎✔ Russion Escorts Service In Delhi Ncrnoida100girls
 

Recently uploaded (20)

Busty Desi⚡Call Girls in Vasundhara Ghaziabad >༒8448380779 Escort Service
Busty Desi⚡Call Girls in Vasundhara Ghaziabad >༒8448380779 Escort ServiceBusty Desi⚡Call Girls in Vasundhara Ghaziabad >༒8448380779 Escort Service
Busty Desi⚡Call Girls in Vasundhara Ghaziabad >༒8448380779 Escort Service
 
Busty Desi⚡Call Girls in Surajpur Greater Noida >༒8448380779 Escort Service
Busty Desi⚡Call Girls in Surajpur Greater Noida  >༒8448380779 Escort ServiceBusty Desi⚡Call Girls in Surajpur Greater Noida  >༒8448380779 Escort Service
Busty Desi⚡Call Girls in Surajpur Greater Noida >༒8448380779 Escort Service
 
Busty Desi⚡Call Girls in Vasundhara Ghaziabad >༒8448380779 Escort Service
Busty Desi⚡Call Girls in Vasundhara Ghaziabad >༒8448380779 Escort ServiceBusty Desi⚡Call Girls in Vasundhara Ghaziabad >༒8448380779 Escort Service
Busty Desi⚡Call Girls in Vasundhara Ghaziabad >༒8448380779 Escort Service
 
GENUINE Babe,Call Girls IN Dwarka Delhi | +91-8377087607
GENUINE Babe,Call Girls IN Dwarka Delhi | +91-8377087607GENUINE Babe,Call Girls IN Dwarka Delhi | +91-8377087607
GENUINE Babe,Call Girls IN Dwarka Delhi | +91-8377087607
 
Enjoy Night ≽ 8448380779 ≼ Call Girls In Gurgaon Sector 54 (Gurgaon)
Enjoy Night ≽ 8448380779 ≼ Call Girls In Gurgaon Sector 54 (Gurgaon)Enjoy Night ≽ 8448380779 ≼ Call Girls In Gurgaon Sector 54 (Gurgaon)
Enjoy Night ≽ 8448380779 ≼ Call Girls In Gurgaon Sector 54 (Gurgaon)
 
Enjoy Night⚡Call Girls Mayur Vihar Delhi >༒8448380779 Escort Service
Enjoy Night⚡Call Girls Mayur Vihar Delhi >༒8448380779 Escort ServiceEnjoy Night⚡Call Girls Mayur Vihar Delhi >༒8448380779 Escort Service
Enjoy Night⚡Call Girls Mayur Vihar Delhi >༒8448380779 Escort Service
 
Enjoy Night ≽ 8448380779 ≼ Call Girls In Gurgaon Sector 57 (Gurgaon)
Enjoy Night ≽ 8448380779 ≼ Call Girls In Gurgaon Sector 57 (Gurgaon)Enjoy Night ≽ 8448380779 ≼ Call Girls In Gurgaon Sector 57 (Gurgaon)
Enjoy Night ≽ 8448380779 ≼ Call Girls In Gurgaon Sector 57 (Gurgaon)
 
Sector 4, Noida Call girls :8448380779 Model Escorts | 100% verified
Sector 4, Noida Call girls :8448380779 Model Escorts | 100% verifiedSector 4, Noida Call girls :8448380779 Model Escorts | 100% verified
Sector 4, Noida Call girls :8448380779 Model Escorts | 100% verified
 
Busty Desi⚡Call Girls in Pari Chowk Escorts, Noida >༒8448380779 Escort Service
Busty Desi⚡Call Girls in Pari Chowk Escorts, Noida >༒8448380779 Escort ServiceBusty Desi⚡Call Girls in Pari Chowk Escorts, Noida >༒8448380779 Escort Service
Busty Desi⚡Call Girls in Pari Chowk Escorts, Noida >༒8448380779 Escort Service
 
Busty Desi⚡Call Girls in Surajpur Greater Noida >༒8448380779 Escort Service
Busty Desi⚡Call Girls in Surajpur Greater Noida >༒8448380779 Escort ServiceBusty Desi⚡Call Girls in Surajpur Greater Noida >༒8448380779 Escort Service
Busty Desi⚡Call Girls in Surajpur Greater Noida >༒8448380779 Escort Service
 
Enjoy Night ≽ 8448380779 ≼ Call Girls In Gurgaon Sector 53 (Gurgaon)
Enjoy Night ≽ 8448380779 ≼ Call Girls In Gurgaon Sector 53 (Gurgaon)Enjoy Night ≽ 8448380779 ≼ Call Girls In Gurgaon Sector 53 (Gurgaon)
Enjoy Night ≽ 8448380779 ≼ Call Girls In Gurgaon Sector 53 (Gurgaon)
 
Busty Desi⚡Call Girls in Pari Chowk Escorts, Noida >༒8448380779 Escort Service
Busty Desi⚡Call Girls in Pari Chowk Escorts, Noida >༒8448380779 Escort ServiceBusty Desi⚡Call Girls in Pari Chowk Escorts, Noida >༒8448380779 Escort Service
Busty Desi⚡Call Girls in Pari Chowk Escorts, Noida >༒8448380779 Escort Service
 
call girls in Indirapuram (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service...
call girls in Indirapuram (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service...call girls in Indirapuram (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service...
call girls in Indirapuram (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service...
 
Busty Desi⚡Call Girls in Pari Chowk Escorts, Noida >༒8448380779 Escort Service
Busty Desi⚡Call Girls in Pari Chowk Escorts, Noida >༒8448380779 Escort ServiceBusty Desi⚡Call Girls in Pari Chowk Escorts, Noida >༒8448380779 Escort Service
Busty Desi⚡Call Girls in Pari Chowk Escorts, Noida >༒8448380779 Escort Service
 
Enjoy Night ≽ 8448380779 ≼ Call Girls In Gurgaon Sector 56 (Gurgaon)
Enjoy Night ≽ 8448380779 ≼ Call Girls In Gurgaon Sector 56 (Gurgaon)Enjoy Night ≽ 8448380779 ≼ Call Girls In Gurgaon Sector 56 (Gurgaon)
Enjoy Night ≽ 8448380779 ≼ Call Girls In Gurgaon Sector 56 (Gurgaon)
 
Sector 10, Noida Call girls :8448380779 Model Escorts | 100% verified
Sector 10, Noida Call girls :8448380779 Model Escorts | 100% verifiedSector 10, Noida Call girls :8448380779 Model Escorts | 100% verified
Sector 10, Noida Call girls :8448380779 Model Escorts | 100% verified
 
Enjoy Night ≽ 8448380779 ≼ Call Girls In Gurgaon Sector 66 (Gurgaon)
Enjoy Night ≽ 8448380779 ≼ Call Girls In Gurgaon Sector 66 (Gurgaon)Enjoy Night ≽ 8448380779 ≼ Call Girls In Gurgaon Sector 66 (Gurgaon)
Enjoy Night ≽ 8448380779 ≼ Call Girls In Gurgaon Sector 66 (Gurgaon)
 
Enjoy Night ≽ 8448380779 ≼ Call Girls In Gurgaon Sector 52 (Gurgaon)
Enjoy Night ≽ 8448380779 ≼ Call Girls In Gurgaon Sector 52 (Gurgaon)Enjoy Night ≽ 8448380779 ≼ Call Girls In Gurgaon Sector 52 (Gurgaon)
Enjoy Night ≽ 8448380779 ≼ Call Girls In Gurgaon Sector 52 (Gurgaon)
 
Dubai Call Girls Centerfold O525547819 Call Girls Dubai Cfnm
Dubai Call Girls Centerfold O525547819 Call Girls Dubai CfnmDubai Call Girls Centerfold O525547819 Call Girls Dubai Cfnm
Dubai Call Girls Centerfold O525547819 Call Girls Dubai Cfnm
 
Call Girls In Vasant Kunj ☎✔ 9871031762 ☎✔ Russion Escorts Service In Delhi Ncr
Call Girls In Vasant Kunj ☎✔ 9871031762 ☎✔ Russion Escorts Service In Delhi NcrCall Girls In Vasant Kunj ☎✔ 9871031762 ☎✔ Russion Escorts Service In Delhi Ncr
Call Girls In Vasant Kunj ☎✔ 9871031762 ☎✔ Russion Escorts Service In Delhi Ncr
 

Dependency Injection Why is it awesome and Why should I care?

  • 1. Dependency Injection: Why is it awesome and why should I care? Nolan Erck Adobe CF Summit 2014
  • 2. About Me ● Consultant (southofshasta.com) ● Software Development, On-site Training, Design ● Using ColdFusion since 1999 (4.x) ● Other stuff: ColdFusion, C++, Java, jQuery, PHP, .NET, HTML5, Android, you get the idea... ● Manager of SacInteractive ● Yes, I will speak at your UG. ● Reformed Video Game Developer. ● Music Junkie
  • 3. Today's Agenda ● What is Dependency Injection? ● When/why do I want it in my projects? ● Intro to Bean Management. ● Intro to Aspect Oriented Programming (AOP). ● Intro to Inversion of Control (IOC). ● Other info. ● Questions. ● Lunch!
  • 4. Prerequisites ● Have some experience building classes and objects. ● Don't need to be an expert at it. ● Don't need to have used an MVC framework. ● DI frameworks can be totally separate. ● And 1 more thing you need to know...
  • 5. Prerequisites ● Object Oriented Programming is hard. ● And some of it is confusing at first. ● And that's NORMAL.
  • 7. What is Dependency Injection? ● A “design pattern” ● $6 phrase for “a way to organize your classes so they work together in a flexible way”. ● Sort of like a for() loop, if(), array[]. – Reusable technique, based around objects. ● There are lots of design patterns – Dependency Injection is 1 design pattern – Inversion of Control (IOC), Aspect Oriented Programming (AOP) and Bean Management are all “variations on that theme”. – There are lots of others too. ● Book: “Head First Design Patterns”
  • 8. What is Dependency Injection? ● It's not platform specific. ● D/I libraries available for ColdFusion, Java, .NET, JavaScript, etc. ● For today's presentation, I'm using a mix of pseudocode and condensed examples from various languages. ● Some of the keywords will change, all the concepts work the same across the libraries and languages.
  • 9. Why / When Do I Want D/I In My Projects? ● Short answer: When you have lots of classes that talk to each other, want to keep code organized, and make it easy to swap different “types” of objects in/out of your application.
  • 10. Why / When Do I Want D/I In My Projects? ● Short answer: When you have lots of classes that talk to each other, want to keep code organized, and make it easy to swap different “types” of objects in/out of your application. ● Longer answer: That's what the rest of the slides are for...
  • 11. Does this mean... ● I have to rebuild my entire app to use D/I? ● No. But you might get more benefits if you make a few changes to legacy code along the way. ● I have to learn a new way to code all my classes? ● No. You can leave everything as-is. – The nice thing about D/I is you can use it or not use it. If you want to write code the “old way” in part of your app, there's nothing stopping you from doing so. It will all still work as-is. ● Neat huh? Let's learn a bit more, shall we?
  • 13. What's a Bean? A “thing” your D/I framework knows about. class User{ … } class DateUtils {…} In your DI framework: bean id=”myUser” class=”User” bean id=”myDateUtils” class=”DateUtils” (Exact syntax varies between DI libraries.)
  • 14. Bean Management class A { public class A() {...} } class B{ public class B( A a ) { … } } class C { public class C( B b ) { … } } class D{ public class D( C c ) {...} } So if I -JUST- want a D object made...
  • 15. Bean Management I have to do all this... A a = new A(); B b = new B( a ); C c = new C( b ); D d = new D( c );
  • 16. Bean Management I have to do all this... A a = new A(); B b = new B( a ); C c = new C( b ); D d = new D( c ); 4 lines of code. The first 3 of which are just “boilerplate” to create the variables I need to make a D. Multiply that out over all the objects in your app... – = a LOT of boilerplate code!
  • 17. Bean Management I have to do all this... A a = new A(); B b = new B( a ); C c = new C( b ); D d = new D( c ); 4 lines of code. The first 3 of which are just “boilerplate” to create the variables I need to make a D. Multiply that out over all the objects in your app... = a LOT of boilerplate code! Another way to say it: “D depends on A, B and C being made first”.
  • 18. Bean Management With a D/I library that does Bean Management, you'd write something like this instead... D d = ColdSpring.getBean( “D” );
  • 19. Bean Management With a D/I library that does Bean Management, you'd write something like this instead... D d = ColdSpring.getBean( “D” ); 4 lines of code just became 1. And no more boilerplate stuff! You tell your D/I framework “I need a D. Please go find out what classes D depends on, make those as needed, and hand me back the final D when you're done.” (“ColdSpring” is kind of a placeholder here for any D/I framework. They all work about the same.)
  • 20. Bean Management ● Quasi Real World Example ● Mailing department wide messages. ● Mail Service CFC ● Needs a Department CFC to know which Department is being emailed ● Department needs a Department DAO for it's SQL stuff. ● And a SecurityLogin CFC to make sure it's validated/logged in/whatever.
  • 21. Bean Management Old Way: mySecLogin = CreateObject( “component”, “SecurityLogin”); myDeptDAO = CreateObject( “component”, “DepartmentDAO” ); dept = CreateObject( “component”, “Department” ); dept.init( mySecLogin, myDeptDAO ); mailSvc = CreateObject( “component”, “MailService” ); mailSvc.init( dept ); mailSvc.sendEmail();
  • 22. Bean Management ● New Way DIFwrk.getBean( “mailService” ).sendMail();
  • 23. Bean Management ● New Way DIFwrk.getBean( “mailService” ).sendMail(); Yes, 1 line of code. Yes, it is THAT easy. (I'm over simplifying, a LITTLE, for effect.)
  • 24. Bean Management ● So how does the D/I framework “know” that I need an A, B and C to make a D object? ● Varies a little from framework to framework. ● Configuration based: XML file that “describes” the relationship between the objects. ● (ColdSpring, Spring) – Nice for, say, white-box testing w/ hands-on QA team. ● Convention based: ● Works automatically by which folders you store classes in, and how the classes are named. ● DI/1, WireBox ● Just pick one and try it out, they all do the same core “things”.
  • 26. Inversion of Control (IOC) ● Fancy term for “flipping” some code around from the “traditional” or “normal” way you'd build objects. ● Instead of building an Employee object, then a Department object, then putting the Employee IN the Department. ● You say “get me a fully populated/constructed Department”. ● Magic happens, automatically building the Employee object for you.
  • 27. Inversion Of Control (IOC) class RockGroup { private String groupName; public String getGroupName(){ return groupName; } public void setGroupName( String name ) { this.groupName = name; } } No Constructor. And groupName is private. So we have to call the setter to initialize the variable with something.
  • 28. Inversion Of Control (IOC) With regular code, we'd do this: String myBandName = “Depeche Mode”; RockGroup rg = new RockGroup(); rg.setGroupName( myBandName ); That works but... Every time I want to provide a different default value, I have to change code, and recompile.
  • 29. Inversion Of Control (IOC) What if we could provide that default value in a “config file”, outside of code? And whenever we create a RockGroup object, it automatically gets a default value for groupName passed in at that instant? You can!
  • 30. Inversion Of Control (IOC) Using a D/I framework, you can have a “config” that notes default values for your classes: <class name=”RockGroup”> <property name=”groupName” value=”Depeche Mode” /> </class> rg = ColdSpring.getBean( “RockGroup” ) ...any time I make a RockGroup, it will automatically initialize the “groupName” variable to the default value I provided. No extra lines of code. And changing this default is now done in a config file. No recompiling code to change it!
  • 31. Inversion Of Control (IOC) What about classes inside of other classes ● AKA “Composition” or the “has a” relationship. ● Kind of the same thing...
  • 32. Inversion Of Control (IOC) class RockGroup{ private String groupName; public String getGroupName() { … } public void setGroupName( String name ) {…} private Musician singer; public Musician getSinger() { … } public void setSinger( Musician m ) {…} } class Musician{ private String name; private int yearsSober; private int age; public String getName() { return name; } public void setName( String n ) { this.name = n; } public int getYearsSober() { … } public void setYearsSober(int y) { … } public void setAge( int a ){...} public int getAge(){ return age; } }
  • 33. Inversion of Control (IOC) With straight code... Musician m = new Musician(); m.name = “David Lee Roth”; m.age = 50; m.yearsSober = 10; RockGroup rg = new RockGroup(); rg.setGroupName( “Van Halen” ); rg.setSinger( m );
  • 34. Inversion of Control (IOC) With D/I... RockGroup rg = Spring.getBean(“RockGroup”);
  • 35. Inversion Of Control (IOC) The Musician class has a String property, that gets initialized just like the String property in RockGroup. Ditto for the “int” properties, etc: Spring.getBean( “Musician” ); – “name” will always be whatever is in my config for the Musician class. <class name=”Musician”> <property name=”name” value=”David Lee Roth” /> <property name=”age” value=”50” /> <property name=”yearsSober” value=”10” /> </class>
  • 36. Inversion Of Control (IOC) You can also refer to other classes in the config, and “inject” them into each other, just like we “injected” the String and int into Musician: <class name=”Singer” id=”mySingerBean”> <property name=”name” value=”David Lee Roth” /> <property name=”yearsSober” value=”10” /> <property name=”age” value=”50” /> </class> <class name=”RockGroup”> <property name=”groupName” value=”Van Halen” /> <property name=”singer” ref=”mySingerBean” /> </class>
  • 37. Inversion Of Control (IOC) So now when I do this... rg = DIFramework.getBean( “RockGroup” ); ● rg will be fully constructed... ● It will have a “groupName” property set to “Van Halen” ● It will have a “singer” property set to “David Lee Roth”, he'll be 50 years old and 10 years sober. ● I did that all with 1 line of code, no boilerplate stuff. ● If I want to change those defaults, I don't have to recompile, I just change a “config” setting.
  • 38. Inversion Of Control (IOC) Swapping out one type of Musician for another doesn't require recompiling code, just change a config <class name=”Singer” id=”singerBean2”> <property name=”name” value=”Sammy Hagar” /> <property name=”yearsSober” value=”15” /> <property name=”age” value=”51” /> </class> <class name=”RockGroup”> <property name=”groupName” value=”Van Halen” /> <property name=”singer” ref=”singerBean2” /> </class>
  • 39. Inversion Of Control (IOC) Swapping out one type of Musician for another doesn't require recompiling code, just change a config <class name=”Singer” id=”singerBean2”> <property name=”name” value=”Sammy Hagar” /> <property name=”yearsSober” value=”15” /> <property name=”age” value=”51” /> </class> <class name=”RockGroup”> <property name=”groupName” value=”Van Halen” /> <property name=”singer” ref=”singerBean2” /> </class> Off topic: discussing if Dave is better/worse than Sammy.
  • 41. Aspect Oriented Programming (AOP) ● AKA “Cross Cutting” ● Change the “aspect” of how a function is called. ● Say I have a foo() method. ● Any time foo() is called... ● Automatically... – Call code before or after foo() – Wrap some sort of code “around” foo() ● e.g. try/catch blocks ● It no longer just does “foo()” ● Also does whatever you define as an aspect of calling foo().
  • 42. Aspect Oriented Programming (AOP) ● Example: drawSalesReport() ● In your AOP library: <func name=”drawSalesReport”> <aspect before run=”checkIfLoggedIn” /> <aspect after run=”saveToDebugLogFile” /> </func>
  • 43. Aspect Oriented Programming (AOP) ● In your code, it USED to look like this: checkIfLoggedIn(); drawSalesReport( UserID=555, dtCreated='1/1/2015' ); SaveToDebugLogFile(); What happens NOW is... DIFramework.runWithAdvice( “drawSalesReport”, { UserID=555, dtCreated='1/1/2015' } ); and that does all 3 things, in correct order.
  • 44. Aspect Oriented Programming (AOP) ● But wait, there's more! ● Can also wrap “blocks” of code around each other, not just function calls before/after each other. ● e.g. try/catch blocks.
  • 45. Aspect Oriented Programming (AOP) Say I have a query like so: <cfquery name=”qryGetUsers”> SELECT * FROM Users </cfquery> In Production, I want that wrapped in a try/catch, but in QA/Development, I don't. (So I can see the debug output, etc.)
  • 46. Aspect Oriented Programming (AOP) I'd have to set some kind of “flag”: <cfif isProduction> <cftry> <cfquery name=”qryGetUsers”> SELECT * FROM Users </cfquery> <cfcatch> <cflog … /> </cfcatch> </cftry> <cfelse> <cfquery name=”qryGetUsers”> SELECT * FROM Users </cfquery> </cfif> Duplicate Code! Remember the DRY rule!
  • 47. Aspect Oriented Programming (AOP) ● Instead of calling a getUsers() method directly: ● GetUsers(); ● In your AOP library: <advice around functionName=”getUsersWithAdvice”> <method name=”getUsers” /> </advice>
  • 48. Aspect Oriented Programming (AOP) <advice around functionName=”getUsersWithAdvice”> – <method name=”getUsers” /> </advice> function getUsersWithAdvice( funcToRun ) { if( isProduction ) { try{ #funcToRun#(); } catch{ … } }else{ #funcToRun#(); }
  • 49. Aspect Oriented Programming (AOP) In your code: DIFramework.runWithAdvice( “getUsers” ); ● The if() statement is still there but our duplicate SQL is gone!
  • 50. So what's the catch? ● This is maybe a new way of thinking about how you build your classes. ● Takes some getting used to. ● A little extra “set up” work ● Naming conventions, typing some XML ● Debugging errors can be slower at first. ...but it does make your code more flexible.
  • 51. Remember... ● You don't have to do this all at once. ● Start with (for example) Bean Management. ● Add other bits as you get comfortable. ● It's not like an MVC framework where the WHOLE app has to be considered. ● Find 1 tiny spot in your app, add a little DI there. ● Add more in increments, go as you learn.
  • 52. Also Remember... ● OO Programming is hard. ● It's different than Procedural code. ● Takes getting used to. ● That's NORMAL. ● Nobody learns all this stuff instantly. ● It takes some time. ● (But it is the way most languages are going these days.)
  • 53. Other Resources ● Book: Spring In Action (Java) ● Book: Head First Design Patterns ● Framework/1, DI/1 and AOP/1 ● ColdSpring documentation ● Good examples, not overwhelming. ● SpringByExample.org ● Java code, but explanation of the general concepts is pretty good. ● I'm guessing the ColdBox docs are also great for this too. Those guys like to write!
  • 54. Questions? Comments? Ways to reach me... nolan@southofshasta.com @southofshasta www.southofshasta.com Thanks!
  • 55. Dependency Injection: Why is it awesome and why should I care? Nolan Erck Adobe CF Summit 2014
  • 56. About Me ● Consultant (southofshasta.com) ● Software Development, On-site Training, Design ● Using ColdFusion since 1999 (4.x) ● Other stuff: ColdFusion, C++, Java, jQuery, PHP, .NET, HTML5, Android, you get the idea... ● Manager of SacInteractive ● Yes, I will speak at your UG. ● Reformed Video Game Developer. ● Music Junkie
  • 57. Today's Agenda ● What is Dependency Injection? ● When/why do I want it in my projects? ● Intro to Bean Management. ● Intro to Aspect Oriented Programming (AOP). ● Intro to Inversion of Control (IOC). ● Other info. ● Questions. ● Lunch!
  • 58. Prerequisites ● Have some experience building classes and objects. ● Don't need to be an expert at it. ● Don't need to have used an MVC framework. ● DI frameworks can be totally separate. ● And 1 more thing you need to know...
  • 59. Prerequisites ● Object Oriented Programming is hard. ● And some of it is confusing at first. ● And that's NORMAL.
  • 61. What is Dependency Injection? ● A “design pattern” ● $6 phrase for “a way to organize your classes so they work together in a flexible way”. ● Sort of like a for() loop, if(), array[]. – Reusable technique, based around objects. ● There are lots of design patterns – Dependency Injection is 1 design pattern – Inversion of Control (IOC), Aspect Oriented Programming (AOP) and Bean Management are all “variations on that theme”. – There are lots of others too. ● Book: “Head First Design Patterns”
  • 62. What is Dependency Injection? ● It's not platform specific. ● D/I libraries available for ColdFusion, Java, .NET, JavaScript, etc. ● For today's presentation, I'm using a mix of pseudocode and condensed examples from various languages. ● Some of the keywords will change, all the concepts work the same across the libraries and languages.
  • 63. Why / When Do I Want D/I In My Projects? ● Short answer: When you have lots of classes that talk to each other, want to keep code organized, and make it easy to swap different “types” of objects in/out of your application.
  • 64. Why / When Do I Want D/I In My Projects? ● Short answer: When you have lots of classes that talk to each other, want to keep code organized, and make it easy to swap different “types” of objects in/out of your application. ● Longer answer: That's what the rest of the slides are for...
  • 65. Does this mean... ● I have to rebuild my entire app to use D/I? ● No. But you might get more benefits if you make a few changes to legacy code along the way. ● I have to learn a new way to code all my classes? ● No. You can leave everything as-is. – The nice thing about D/I is you can use it or not use it. If you want to write code the “old way” in part of your app, there's nothing stopping you from doing so. It will all still work as-is. ● Neat huh? Let's learn a bit more, shall we?
  • 67. What's a Bean? A “thing” your D/I framework knows about. class User{ … } class DateUtils {…} In your DI framework: bean id=”myUser” class=”User” bean id=”myDateUtils” class=”DateUtils” (Exact syntax varies between DI libraries.)
  • 68. Bean Management class A { public class A() {...} } class B{ public class B( A a ) { … } } class C { public class C( B b ) { … } } class D{ public class D( C c ) {...} } So if I -JUST- want a D object made...
  • 69. Bean Management I have to do all this... A a = new A(); B b = new B( a ); C c = new C( b ); D d = new D( c );
  • 70. Bean Management I have to do all this... A a = new A(); B b = new B( a ); C c = new C( b ); D d = new D( c ); 4 lines of code. The first 3 of which are just “boilerplate” to create the variables I need to make a D. Multiply that out over all the objects in your app... – = a LOT of boilerplate code!
  • 71. Bean Management I have to do all this... A a = new A(); B b = new B( a ); C c = new C( b ); D d = new D( c ); 4 lines of code. The first 3 of which are just “boilerplate” to create the variables I need to make a D. Multiply that out over all the objects in your app... = a LOT of boilerplate code! Another way to say it: “D depends on A, B and C being made first”.
  • 72. Bean Management With a D/I library that does Bean Management, you'd write something like this instead... D d = ColdSpring.getBean( “D” );
  • 73. Bean Management With a D/I library that does Bean Management, you'd write something like this instead... D d = ColdSpring.getBean( “D” ); 4 lines of code just became 1. And no more boilerplate stuff! You tell your D/I framework “I need a D. Please go find out what classes D depends on, make those as needed, and hand me back the final D when you're done.” (“ColdSpring” is kind of a placeholder here for any D/I framework. They all work about the same.)
  • 74. Bean Management ● Quasi Real World Example ● Mailing department wide messages. ● Mail Service CFC ● Needs a Department CFC to know which Department is being emailed ● Department needs a Department DAO for it's SQL stuff. ● And a SecurityLogin CFC to make sure it's validated/logged in/whatever.
  • 75. Bean Management Old Way: mySecLogin = CreateObject( “component”, “SecurityLogin”); myDeptDAO = CreateObject( “component”, “DepartmentDAO” ); dept = CreateObject( “component”, “Department” ); dept.init( mySecLogin, myDeptDAO ); mailSvc = CreateObject( “component”, “MailService” ); mailSvc.init( dept ); mailSvc.sendEmail();
  • 76. Bean Management ● New Way DIFwrk.getBean( “mailService” ).sendMail();
  • 77. Bean Management ● New Way DIFwrk.getBean( “mailService” ).sendMail(); Yes, 1 line of code. Yes, it is THAT easy. (I'm over simplifying, a LITTLE, for effect.)
  • 78. Bean Management ● So how does the D/I framework “know” that I need an A, B and C to make a D object? ● Varies a little from framework to framework. ● Configuration based: XML file that “describes” the relationship between the objects. ● (ColdSpring, Spring) – Nice for, say, white-box testing w/ hands-on QA team. ● Convention based: ● Works automatically by which folders you store classes in, and how the classes are named. ● DI/1, WireBox ● Just pick one and try it out, they all do the same core “things”.
  • 80. Inversion of Control (IOC) ● Fancy term for “flipping” some code around from the “traditional” or “normal” way you'd build objects. ● Instead of building an Employee object, then a Department object, then putting the Employee IN the Department. ● You say “get me a fully populated/constructed Department”. ● Magic happens, automatically building the Employee object for you.
  • 81. Inversion Of Control (IOC) class RockGroup { private String groupName; public String getGroupName(){ return groupName; } public void setGroupName( String name ) { this.groupName = name; } } No Constructor. And groupName is private. So we have to call the setter to initialize the variable with something.
  • 82. Inversion Of Control (IOC) With regular code, we'd do this: String myBandName = “Depeche Mode”; RockGroup rg = new RockGroup(); rg.setGroupName( myBandName ); That works but... Every time I want to provide a different default value, I have to change code, and recompile.
  • 83. Inversion Of Control (IOC) What if we could provide that default value in a “config file”, outside of code? And whenever we create a RockGroup object, it automatically gets a default value for groupName passed in at that instant? You can!
  • 84. Inversion Of Control (IOC) Using a D/I framework, you can have a “config” that notes default values for your classes: <class name=”RockGroup”> <property name=”groupName” value=”Depeche Mode” /> </class> rg = ColdSpring.getBean( “RockGroup” ) ...any time I make a RockGroup, it will automatically initialize the “groupName” variable to the default value I provided. No extra lines of code. And changing this default is now done in a config file. No recompiling code to change it!
  • 85. Inversion Of Control (IOC) What about classes inside of other classes ● AKA “Composition” or the “has a” relationship. ● Kind of the same thing...
  • 86. Inversion Of Control (IOC) class RockGroup{ private String groupName; public String getGroupName() { … } public void setGroupName( String name ) {…} private Musician singer; public Musician getSinger() { … } public void setSinger( Musician m ) {…} } class Musician{ private String name; private int yearsSober; private int age; public String getName() { return name; } public void setName( String n ) { this.name = n; } public int getYearsSober() { … } public void setYearsSober(int y) { … } public void setAge( int a ){...} public int getAge(){ return age; } }
  • 87. Inversion of Control (IOC) With straight code... Musician m = new Musician(); m.name = “David Lee Roth”; m.age = 50; m.yearsSober = 10; RockGroup rg = new RockGroup(); rg.setGroupName( “Van Halen” ); rg.setSinger( m );
  • 88. Inversion of Control (IOC) With D/I... RockGroup rg = Spring.getBean(“RockGroup”);
  • 89. Inversion Of Control (IOC) The Musician class has a String property, that gets initialized just like the String property in RockGroup. Ditto for the “int” properties, etc: Spring.getBean( “Musician” ); – “name” will always be whatever is in my config for the Musician class. <class name=”Musician”> <property name=”name” value=”David Lee Roth” /> <property name=”age” value=”50” /> <property name=”yearsSober” value=”10” /> </class>
  • 90. Inversion Of Control (IOC) You can also refer to other classes in the config, and “inject” them into each other, just like we “injected” the String and int into Musician: <class name=”Singer” id=”mySingerBean”> <property name=”name” value=”David Lee Roth” /> <property name=”yearsSober” value=”10” /> <property name=”age” value=”50” /> </class> <class name=”RockGroup”> <property name=”groupName” value=”Van Halen” /> <property name=”singer” ref=”mySingerBean” /> </class>
  • 91. Inversion Of Control (IOC) So now when I do this... rg = DIFramework.getBean( “RockGroup” ); ● rg will be fully constructed... ● It will have a “groupName” property set to “Van Halen” ● It will have a “singer” property set to “David Lee Roth”, he'll be 50 years old and 10 years sober. ● I did that all with 1 line of code, no boilerplate stuff. ● If I want to change those defaults, I don't have to recompile, I just change a “config” setting.
  • 92. Inversion Of Control (IOC) Swapping out one type of Musician for another doesn't require recompiling code, just change a config <class name=”Singer” id=”singerBean2”> <property name=”name” value=”Sammy Hagar” /> <property name=”yearsSober” value=”15” /> <property name=”age” value=”51” /> </class> <class name=”RockGroup”> <property name=”groupName” value=”Van Halen” /> <property name=”singer” ref=”singerBean2” /> </class>
  • 93. Inversion Of Control (IOC) Swapping out one type of Musician for another doesn't require recompiling code, just change a config <class name=”Singer” id=”singerBean2”> <property name=”name” value=”Sammy Hagar” /> <property name=”yearsSober” value=”15” /> <property name=”age” value=”51” /> </class> <class name=”RockGroup”> <property name=”groupName” value=”Van Halen” /> <property name=”singer” ref=”singerBean2” /> </class> Off topic: discussing if Dave is better/worse than Sammy.
  • 95. Aspect Oriented Programming (AOP) ● AKA “Cross Cutting” ● Change the “aspect” of how a function is called. ● Say I have a foo() method. ● Any time foo() is called... ● Automatically... – Call code before or after foo() – Wrap some sort of code “around” foo() ● e.g. try/catch blocks ● It no longer just does “foo()” ● Also does whatever you define as an aspect of calling foo().
  • 96. Aspect Oriented Programming (AOP) ● Example: drawSalesReport() ● In your AOP library: <func name=”drawSalesReport”> <aspect before run=”checkIfLoggedIn” /> <aspect after run=”saveToDebugLogFile” /> </func>
  • 97. Aspect Oriented Programming (AOP) ● In your code, it USED to look like this: checkIfLoggedIn(); drawSalesReport( UserID=555, dtCreated='1/1/2015' ); SaveToDebugLogFile(); What happens NOW is... DIFramework.runWithAdvice( “drawSalesReport”, { UserID=555, dtCreated='1/1/2015' } ); and that does all 3 things, in correct order.
  • 98. Aspect Oriented Programming (AOP) ● But wait, there's more! ● Can also wrap “blocks” of code around each other, not just function calls before/after each other. ● e.g. try/catch blocks.
  • 99. Aspect Oriented Programming (AOP) Say I have a query like so: <cfquery name=”qryGetUsers”> SELECT * FROM Users </cfquery> In Production, I want that wrapped in a try/catch, but in QA/Development, I don't. (So I can see the debug output, etc.)
  • 100. Aspect Oriented Programming (AOP) I'd have to set some kind of “flag”: <cfif isProduction> <cftry> <cfquery name=”qryGetUsers”> SELECT * FROM Users </cfquery> <cfcatch> <cflog … /> </cfcatch> </cftry> <cfelse> <cfquery name=”qryGetUsers”> SELECT * FROM Users </cfquery> </cfif> Duplicate Code! Remember the DRY rule!
  • 101. Aspect Oriented Programming (AOP) ● Instead of calling a getUsers() method directly: ● GetUsers(); ● In your AOP library: <advice around functionName=”getUsersWithAdvice”> <method name=”getUsers” /> </advice>
  • 102. Aspect Oriented Programming (AOP) <advice around functionName=”getUsersWithAdvice”> – <method name=”getUsers” /> </advice> function getUsersWithAdvice( funcToRun ) { if( isProduction ) { try{ #funcToRun#(); } catch{ … } }else{ #funcToRun#(); }
  • 103. Aspect Oriented Programming (AOP) In your code: DIFramework.runWithAdvice( “getUsers” ); ● The if() statement is still there but our duplicate SQL is gone!
  • 104. So what's the catch? ● This is maybe a new way of thinking about how you build your classes. ● Takes some getting used to. ● A little extra “set up” work ● Naming conventions, typing some XML ● Debugging errors can be slower at first. ...but it does make your code more flexible.
  • 105. Remember... ● You don't have to do this all at once. ● Start with (for example) Bean Management. ● Add other bits as you get comfortable. ● It's not like an MVC framework where the WHOLE app has to be considered. ● Find 1 tiny spot in your app, add a little DI there. ● Add more in increments, go as you learn.
  • 106. Also Remember... ● OO Programming is hard. ● It's different than Procedural code. ● Takes getting used to. ● That's NORMAL. ● Nobody learns all this stuff instantly. ● It takes some time. ● (But it is the way most languages are going these days.)
  • 107. Other Resources ● Book: Spring In Action (Java) ● Book: Head First Design Patterns ● Framework/1, DI/1 and AOP/1 ● ColdSpring documentation ● Good examples, not overwhelming. ● SpringByExample.org ● Java code, but explanation of the general concepts is pretty good. ● I'm guessing the ColdBox docs are also great for this too. Those guys like to write!
  • 108. Questions? Comments? Ways to reach me... nolan@southofshasta.com @southofshasta www.southofshasta.com Thanks!