14. 14
12.09.2015
Code Modularity and Dependency Injection in AngularJS
Solution?
Modular Code
“A Module is a separate, interfaced piece of
code that does stuff you need”
S.Reznik, http://www.hirez.io/
17. 17
12.09.2015
Code Modularity and Dependency Injection in AngularJS
SoC
“ A program that embodies SoC well is called
a modular program. Modularity, and hence
separation of concerns, is achieved by
encapsulating information inside a section
of code that has a well-defined interface.
Encapsulation is a means of information
hiding.”
~Wikipedia
OBJECT ORIENTED HTML-CSS-JSMVC
18. 18
12.09.2015
Code Modularity and Dependency Injection in AngularJS
More Rules
• Single Responsibility Principle
• Law of Demeter
Dumber Components === Maintainable Code
21. 21
12.09.2015
Code Modularity and Dependency Injection in AngularJS
Modular Code
Writing modular code:
• Divide pieces of code into Objects
• Create a “Builder”
And then we couple them together
22. 22
12.09.2015
Code Modularity and Dependency Injection in AngularJS
Modular Code
function gatherWithFriends(){
var beer = new Beer();
…
beer.isEmpty();
}
• Forces me to know how to create the beer
• Tightly coupled to the Implementation Class
• To test – need to monkey patch Beer and remember to
clean it up
23. 23
12.09.2015
Code Modularity and Dependency Injection in AngularJS
Modular Code
function gatherWithFriends(){
//with service locator
var beer = waiter.get(“beer”);
…
beer.isEmpty();
}
• Everyone in the system needs to know about the
waiter(dependency)
• Dependencies are scattered throughout the code
24. 24
12.09.2015
Code Modularity and Dependency Injection in AngularJS
Modular Code - Solution
function gatherWithFriends(beer){
…
beer.isEmpty();
}
• Inject to the constructor
• Easier testing
• One place to see all of the dependencies
25. 25
12.09.2015
Code Modularity and Dependency Injection in AngularJS
Modular Code - Solution
function main() {
var gather = new Gather(
new Beer(new Fermentation(
new Hops, new Malt, new
Water…)))
}
• To much knowledge is in one place
• Doesn’t scale
26. 26
12.09.2015
Dependency Injection
Code Modularity and Dependency Injection in AngularJS
• Dependencies (services) are injected into
a dependent object (client)
• Separates the creation of client’s
dependencies from it’s behavior
• Allows loose coupling and follow single
responsibility principle
DEPENDENCY INJECTION
DESIGN PATTERN
28. 28
12.09.2015
Dependency Injection
Code Modularity and Dependency Injection in AngularJS
CLIENT
I WANT BEER!
INJECTOR
SERVICE CREATORS
DO I KNOW HOW
TO CREATE BEER?
GIVE ME BEER
YES, I KNOW!
GIVE ME “BEER CREATOR”
HERE’S YOUR BEER
HERE YOU GO,
MATE
$get()
29. 29
12.09.2015
Code Modularity and Dependency Injection in AngularJS
Dependency Injection in AngularJS
Dependency Injection in AngularJS
30. 30
12.09.2015
Code Modularity and Dependency Injection in AngularJS
Dependency Injection in AngularJS
Our app needs objects
1. Someone needs to
instantiate them
2. …and to wire them
up
31. 31
12.09.2015
Code Modularity and Dependency Injection in AngularJS
Injector Service
Components
Specialized Objects:
• CONTROLLERS
• DIRECTIVES
• FILTERS
Services:
• PROVIDER
• FACTORY
• SERVICE
• VALUE
• CONSTANTnon-injectable
injectable
Service
Creators
32. 32
12.09.2015
Code Modularity and Dependency Injection in AngularJS
Code snippet
.controller('myCtrl',
function(beer) {
beer.isEmpty();
});
BEST PRACTICE
.controller('myCtrl',
['com.restaurant.beverages.draughtBeer',
function(beer) {
beer.isEmpty();
}]);
33. 33
12.09.2015
Code Modularity and Dependency Injection in AngularJS
Service
SERVICE, FACTORY
• Injectable – can be injected into other
components
• Singleton – only one instance
• A place to store your state
• Created by a Provider’s $get() method
34. 34
12.09.2015
Code Modularity and Dependency Injection in AngularJS
Provider
INJECTOR
PROVIDER
Now I’ll call $get()
and I’ll be on my way
GIVE ME “BEER PROVIDER” beerProvider
GIVE ME BEER
HERE’S YOUR BEER
ABSOLUTELY
SOBER CLIENT
PROVIDER
• $get()
• Gives you the ability to configure the
service on Angular bootstrap
• An object that provides the creator for a
service
36. 36
12.09.2015
Code Modularity and Dependency Injection in AngularJS
Injection
INJECTION PROCESS
• $injector gets an injection request
• Search the service instances cache
• If there isn’t any – lookup for the relevant
Provider
• Call the Provider’s $get() method
• Save the result to the service instances
cache
• Inject the result (Singleton) to the client
37. 37
12.09.2015
Code Modularity and Dependency Injection in AngularJS
Injection
$injector
providersCacheservicesCache
.run(function(beerService){})
40. 40
12.09.2015
Code Modularity and Dependency Injection in AngularJS
angular.module()
angular.module()
• Think of it as a ‘Definition Container’
• … or like package for Classes
• Collection of components definitions
41. 41
12.09.2015
Code Modularity and Dependency Injection in AngularJS
ng-app
• Bootstrap – creates the $injector
• Runs the aggregated .configs
• Runs the aggregated .runs
NG APP LIFE CYCLE
42. 42
12.09.2015
Code Modularity and Dependency Injection in AngularJS
ng-app
• Can only inject Providers & Constants
• It is the place to configure a service before
it gets instantiated
.config()
43. 43
12.09.2015
Code Modularity and Dependency Injection in AngularJS
ng-app
• Invoked after all the .config()s got
executed
• Can inject services
.run()
44. 44
12.09.2015
Code Modularity and Dependency Injection in AngularJS
Snippets - .module
Module Dependencies
• Setter with [dependencies, …]
• Getter without the dependencies array […]