SlideShare ist ein Scribd-Unternehmen logo
1 von 51
1
12.09.2015
Code Modularity and Dependency
Injection in AngularJS
Lyubomir Vetskov
3
12.09.2015
• Separation of Concerns
• Modular Code
• Dependency Injection
• Angular DI Implementation
• Angular Modules
• Services API
Today’s Agenda:
Code Modularity and Dependency Injection in AngularJS
4
12.09.2015
Code Modularity and Dependency Injection in AngularJS
Why DI?
{…}
{…}
5
12.09.2015
Code Modularity and Dependency Injection in AngularJS
We want a beer.
6
12.09.2015
Code Modularity and Dependency Injection in AngularJS
…and fermentation…
7
12.09.2015
Code Modularity and Dependency Injection in AngularJS
But can’t get a beer without hops…
8
12.09.2015
Code Modularity and Dependency Injection in AngularJS
…and without malt…
9
12.09.2015
Code Modularity and Dependency Injection in AngularJS
…water…
10
12.09.2015
Code Modularity and Dependency Injection in AngularJS
…uhm…
11
12.09.2015
Code Modularity and Dependency Injection in AngularJS
Confused?
All we wanted is beer. Why do we need to
know all those things?
12
12.09.2015
Code Modularity and Dependency Injection in AngularJS
The old time solution
Use 1 HUGE file
13
12.09.2015
Code Modularity and Dependency Injection in AngularJS
The old time solution
Large Apps === Complexity
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/
15
12.09.2015
Code Modularity and Dependency Injection in AngularJS
Consequences
Modular Code demands:
SEPARATION
OF
CONCERNS
16
12.09.2015
Code Modularity and Dependency Injection in AngularJS
Consequences
Separation Of Concerns
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
12.09.2015
Code Modularity and Dependency Injection in AngularJS
More Rules
• Single Responsibility Principle
• Law of Demeter
Dumber Components === Maintainable Code
19
12.09.2015
Code Modularity and Dependency Injection in AngularJS
Conclusion
Being Modular
means having
Dependencies
20
12.09.2015
Code Modularity and Dependency Injection in AngularJS
Conclusion
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
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
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
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
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
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
27
12.09.2015
Dependency Injection
Code Modularity and Dependency Injection in AngularJS
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
12.09.2015
Code Modularity and Dependency Injection in AngularJS
Dependency Injection in AngularJS
Dependency Injection in AngularJS
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
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
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
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
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
35
12.09.2015
Code Modularity and Dependency Injection in AngularJS
Are you still with me?
PERPLEXED?
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
12.09.2015
Code Modularity and Dependency Injection in AngularJS
Injection
$injector
providersCacheservicesCache
.run(function(beerService){})
38
12.09.2015
Code Modularity and Dependency Injection in AngularJS
Where?
All that is to happen within a
MODULE
39
12.09.2015
Code Modularity and Dependency Injection in AngularJS
Module
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
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
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
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
12.09.2015
Code Modularity and Dependency Injection in AngularJS
Snippets - .module
Module Dependencies
• Setter with [dependencies, …]
• Getter without the dependencies array […]
45
12.09.2015
Code Modularity and Dependency Injection in AngularJS
Snippets - .provider()
.provider()
46
12.09.2015
Code Modularity and Dependency Injection in AngularJS
Snippets - .factory()
.factory()
47
12.09.2015
Code Modularity and Dependency Injection in AngularJS
Snippets - .service()
.service()
48
12.09.2015
Code Modularity and Dependency Injection in AngularJS
Snippets - .value()
.value()
49
12.09.2015
Code Modularity and Dependency Injection in AngularJS
Snippets - .constant()
.constant()
50
12.09.2015
Code Modularity and Dependency Injection in AngularJS
lyubomir.vetskov
@musala.com
51
12.09.2015
Thank You!

Weitere ähnliche Inhalte

Was ist angesagt?

Ng Sydney Dynamic Templates Talk - 18 April 2018
Ng Sydney Dynamic Templates Talk - 18 April 2018Ng Sydney Dynamic Templates Talk - 18 April 2018
Ng Sydney Dynamic Templates Talk - 18 April 2018Roger Kermode
 
Angular coding: from project management to web and mobile deploy
Angular coding: from project management to web and mobile deployAngular coding: from project management to web and mobile deploy
Angular coding: from project management to web and mobile deployCorley S.r.l.
 
Go live with angular 4
Go live with angular 4Go live with angular 4
Go live with angular 4Indra Gunawan
 
Meetup SkillValue - Angular 6 : Bien démarrer son application
Meetup SkillValue - Angular 6 : Bien démarrer son applicationMeetup SkillValue - Angular 6 : Bien démarrer son application
Meetup SkillValue - Angular 6 : Bien démarrer son applicationThibault Even
 
Angular 2 Training | Angular 2 Tutorial For Beginners | Angular Certification...
Angular 2 Training | Angular 2 Tutorial For Beginners | Angular Certification...Angular 2 Training | Angular 2 Tutorial For Beginners | Angular Certification...
Angular 2 Training | Angular 2 Tutorial For Beginners | Angular Certification...Edureka!
 
Spring MVC Intro / Gore - Nov NHJUG
Spring MVC Intro / Gore - Nov NHJUGSpring MVC Intro / Gore - Nov NHJUG
Spring MVC Intro / Gore - Nov NHJUGTed Pennings
 
Angular 4 Introduction Tutorial
Angular 4 Introduction TutorialAngular 4 Introduction Tutorial
Angular 4 Introduction TutorialScott Lee
 
AngularJS Vs Angular: Understanding the Differences
AngularJS Vs Angular: Understanding the DifferencesAngularJS Vs Angular: Understanding the Differences
AngularJS Vs Angular: Understanding the DifferencesTechtic Solutions
 
Developing a Demo Application with Angular 4 - J2I
Developing a Demo Application with Angular 4 - J2IDeveloping a Demo Application with Angular 4 - J2I
Developing a Demo Application with Angular 4 - J2INader Debbabi
 

Was ist angesagt? (14)

Angular Directives
Angular DirectivesAngular Directives
Angular Directives
 
Angular js
Angular jsAngular js
Angular js
 
Ng Sydney Dynamic Templates Talk - 18 April 2018
Ng Sydney Dynamic Templates Talk - 18 April 2018Ng Sydney Dynamic Templates Talk - 18 April 2018
Ng Sydney Dynamic Templates Talk - 18 April 2018
 
What angular 13 will bring to the table
What angular 13 will bring to the table What angular 13 will bring to the table
What angular 13 will bring to the table
 
Angular coding: from project management to web and mobile deploy
Angular coding: from project management to web and mobile deployAngular coding: from project management to web and mobile deploy
Angular coding: from project management to web and mobile deploy
 
Go live with angular 4
Go live with angular 4Go live with angular 4
Go live with angular 4
 
Meetup SkillValue - Angular 6 : Bien démarrer son application
Meetup SkillValue - Angular 6 : Bien démarrer son applicationMeetup SkillValue - Angular 6 : Bien démarrer son application
Meetup SkillValue - Angular 6 : Bien démarrer son application
 
Angular 2 Training | Angular 2 Tutorial For Beginners | Angular Certification...
Angular 2 Training | Angular 2 Tutorial For Beginners | Angular Certification...Angular 2 Training | Angular 2 Tutorial For Beginners | Angular Certification...
Angular 2 Training | Angular 2 Tutorial For Beginners | Angular Certification...
 
Spring MVC Intro / Gore - Nov NHJUG
Spring MVC Intro / Gore - Nov NHJUGSpring MVC Intro / Gore - Nov NHJUG
Spring MVC Intro / Gore - Nov NHJUG
 
Angular 4 Introduction Tutorial
Angular 4 Introduction TutorialAngular 4 Introduction Tutorial
Angular 4 Introduction Tutorial
 
Lecture 32
Lecture 32Lecture 32
Lecture 32
 
AngularJS Vs Angular: Understanding the Differences
AngularJS Vs Angular: Understanding the DifferencesAngularJS Vs Angular: Understanding the Differences
AngularJS Vs Angular: Understanding the Differences
 
Angular js
Angular jsAngular js
Angular js
 
Developing a Demo Application with Angular 4 - J2I
Developing a Demo Application with Angular 4 - J2IDeveloping a Demo Application with Angular 4 - J2I
Developing a Demo Application with Angular 4 - J2I
 

Andere mochten auch

Introdução ao pensamento complexo (capitulo 6)
Introdução ao pensamento complexo (capitulo 6)Introdução ao pensamento complexo (capitulo 6)
Introdução ao pensamento complexo (capitulo 6)Kyllyson Afonso
 
Announcements, 11/27/11
Announcements, 11/27/11Announcements, 11/27/11
Announcements, 11/27/11CLADSM
 
Bring Your Own Coffee webinar Facebook
Bring Your Own Coffee webinar FacebookBring Your Own Coffee webinar Facebook
Bring Your Own Coffee webinar FacebookErik van der Wal
 
Petrobras Biocombustíveis
Petrobras BiocombustíveisPetrobras Biocombustíveis
Petrobras BiocombustíveisCasulo
 
Repent And Be Revived Slides, 5/12/13
Repent And Be Revived Slides, 5/12/13Repent And Be Revived Slides, 5/12/13
Repent And Be Revived Slides, 5/12/13CLADSM
 
Politica publica para las familias de bogota 2011 2025
Politica publica para las familias de bogota 2011 2025Politica publica para las familias de bogota 2011 2025
Politica publica para las familias de bogota 2011 2025Sândrâ Rîncôn
 
選択肢ガイドブック 4
選択肢ガイドブック 4選択肢ガイドブック 4
選択肢ガイドブック 4hkano
 
Tecnologia trabajo ralizado
Tecnologia trabajo ralizadoTecnologia trabajo ralizado
Tecnologia trabajo ralizadoudenar nariño
 
Combitrans Logística da Amazônia
Combitrans Logística da AmazôniaCombitrans Logística da Amazônia
Combitrans Logística da AmazôniaCasulo
 
Living in Focus Slides, 11/8/15
Living in Focus Slides, 11/8/15Living in Focus Slides, 11/8/15
Living in Focus Slides, 11/8/15CLADSM
 
رزومه ویژن تجارت باختر
رزومه ویژن تجارت باختررزومه ویژن تجارت باختر
رزومه ویژن تجارت باخترvahid amini
 
Cuerpo humano, aparatos, sistemas y trastornos.
Cuerpo humano, aparatos, sistemas y trastornos.Cuerpo humano, aparatos, sistemas y trastornos.
Cuerpo humano, aparatos, sistemas y trastornos.Sara Castañeda Mendoza
 
2017 Indiana Beach Event Planning Guide
2017 Indiana Beach Event Planning Guide2017 Indiana Beach Event Planning Guide
2017 Indiana Beach Event Planning GuideKate Huff
 
Pkn sengketa indonesia dan timor leste
Pkn sengketa indonesia dan timor lestePkn sengketa indonesia dan timor leste
Pkn sengketa indonesia dan timor lesteerlin dana
 
Canción con pictogramas : "Lobo feroz".
Canción con pictogramas : "Lobo feroz".Canción con pictogramas : "Lobo feroz".
Canción con pictogramas : "Lobo feroz".AnxoPictogramas Anxo
 
Apresentação empresarial Rio Ave
Apresentação empresarial Rio AveApresentação empresarial Rio Ave
Apresentação empresarial Rio AveCasulo
 
Mi experiencia utpl
Mi experiencia utplMi experiencia utpl
Mi experiencia utplDiany's Love
 

Andere mochten auch (18)

Introdução ao pensamento complexo (capitulo 6)
Introdução ao pensamento complexo (capitulo 6)Introdução ao pensamento complexo (capitulo 6)
Introdução ao pensamento complexo (capitulo 6)
 
Announcements, 11/27/11
Announcements, 11/27/11Announcements, 11/27/11
Announcements, 11/27/11
 
John Havlicek Jr. with Fish
John Havlicek Jr. with FishJohn Havlicek Jr. with Fish
John Havlicek Jr. with Fish
 
Bring Your Own Coffee webinar Facebook
Bring Your Own Coffee webinar FacebookBring Your Own Coffee webinar Facebook
Bring Your Own Coffee webinar Facebook
 
Petrobras Biocombustíveis
Petrobras BiocombustíveisPetrobras Biocombustíveis
Petrobras Biocombustíveis
 
Repent And Be Revived Slides, 5/12/13
Repent And Be Revived Slides, 5/12/13Repent And Be Revived Slides, 5/12/13
Repent And Be Revived Slides, 5/12/13
 
Politica publica para las familias de bogota 2011 2025
Politica publica para las familias de bogota 2011 2025Politica publica para las familias de bogota 2011 2025
Politica publica para las familias de bogota 2011 2025
 
選択肢ガイドブック 4
選択肢ガイドブック 4選択肢ガイドブック 4
選択肢ガイドブック 4
 
Tecnologia trabajo ralizado
Tecnologia trabajo ralizadoTecnologia trabajo ralizado
Tecnologia trabajo ralizado
 
Combitrans Logística da Amazônia
Combitrans Logística da AmazôniaCombitrans Logística da Amazônia
Combitrans Logística da Amazônia
 
Living in Focus Slides, 11/8/15
Living in Focus Slides, 11/8/15Living in Focus Slides, 11/8/15
Living in Focus Slides, 11/8/15
 
رزومه ویژن تجارت باختر
رزومه ویژن تجارت باختررزومه ویژن تجارت باختر
رزومه ویژن تجارت باختر
 
Cuerpo humano, aparatos, sistemas y trastornos.
Cuerpo humano, aparatos, sistemas y trastornos.Cuerpo humano, aparatos, sistemas y trastornos.
Cuerpo humano, aparatos, sistemas y trastornos.
 
2017 Indiana Beach Event Planning Guide
2017 Indiana Beach Event Planning Guide2017 Indiana Beach Event Planning Guide
2017 Indiana Beach Event Planning Guide
 
Pkn sengketa indonesia dan timor leste
Pkn sengketa indonesia dan timor lestePkn sengketa indonesia dan timor leste
Pkn sengketa indonesia dan timor leste
 
Canción con pictogramas : "Lobo feroz".
Canción con pictogramas : "Lobo feroz".Canción con pictogramas : "Lobo feroz".
Canción con pictogramas : "Lobo feroz".
 
Apresentação empresarial Rio Ave
Apresentação empresarial Rio AveApresentação empresarial Rio Ave
Apresentação empresarial Rio Ave
 
Mi experiencia utpl
Mi experiencia utplMi experiencia utpl
Mi experiencia utpl
 

Ähnlich wie Presentation_muffin_conference_2015

Step by Step - AngularJS
Step by Step - AngularJSStep by Step - AngularJS
Step by Step - AngularJSInfragistics
 
Angularjs overview
Angularjs  overviewAngularjs  overview
Angularjs overviewVickyCmd
 
Angular js getting started
Angular js getting startedAngular js getting started
Angular js getting startedHemant Mali
 
AngularJS: Service, factory & provider
AngularJS: Service, factory & providerAngularJS: Service, factory & provider
AngularJS: Service, factory & providerCorley S.r.l.
 
Getting Started With AngularJS
Getting Started With AngularJSGetting Started With AngularJS
Getting Started With AngularJSEdureka!
 
Introduction to SPAs with AngularJS
Introduction to SPAs with AngularJSIntroduction to SPAs with AngularJS
Introduction to SPAs with AngularJSLaurent Duveau
 
Getting Started with AngularJS
Getting Started with AngularJSGetting Started with AngularJS
Getting Started with AngularJSEdureka!
 
AngularJS for Beginners
AngularJS for BeginnersAngularJS for Beginners
AngularJS for BeginnersEdureka!
 
Introduction to AngularJS - More Than Just Data Binding
Introduction to AngularJS - More Than Just Data BindingIntroduction to AngularJS - More Than Just Data Binding
Introduction to AngularJS - More Than Just Data BindingLogical Advantage
 
Introduction of ASP.NET MVC and AngularJS
Introduction of ASP.NET MVC and AngularJSIntroduction of ASP.NET MVC and AngularJS
Introduction of ASP.NET MVC and AngularJSMohamed Elkhodary
 
What are the key distinctions between Angular and AngularJS?
What are the key distinctions between Angular and AngularJS?What are the key distinctions between Angular and AngularJS?
What are the key distinctions between Angular and AngularJS?Albiorix Technology
 
AngularJs (1.x) Presentation
AngularJs (1.x) PresentationAngularJs (1.x) Presentation
AngularJs (1.x) PresentationRaghubir Singh
 

Ähnlich wie Presentation_muffin_conference_2015 (20)

AngularJS
AngularJSAngularJS
AngularJS
 
What's new in Angular 2?
What's new in Angular 2?What's new in Angular 2?
What's new in Angular 2?
 
Step by Step - AngularJS
Step by Step - AngularJSStep by Step - AngularJS
Step by Step - AngularJS
 
Angularjs overview
Angularjs  overviewAngularjs  overview
Angularjs overview
 
Angular
AngularAngular
Angular
 
Angular js getting started
Angular js getting startedAngular js getting started
Angular js getting started
 
AngularJS: Service, factory & provider
AngularJS: Service, factory & providerAngularJS: Service, factory & provider
AngularJS: Service, factory & provider
 
Getting Started With AngularJS
Getting Started With AngularJSGetting Started With AngularJS
Getting Started With AngularJS
 
Introduction to SPAs with AngularJS
Introduction to SPAs with AngularJSIntroduction to SPAs with AngularJS
Introduction to SPAs with AngularJS
 
Getting Started with AngularJS
Getting Started with AngularJSGetting Started with AngularJS
Getting Started with AngularJS
 
AngularJS for Beginners
AngularJS for BeginnersAngularJS for Beginners
AngularJS for Beginners
 
AngularJS is awesome
AngularJS is awesomeAngularJS is awesome
AngularJS is awesome
 
Introduction to Angular JS
Introduction to Angular JSIntroduction to Angular JS
Introduction to Angular JS
 
Ajs ppt
Ajs pptAjs ppt
Ajs ppt
 
Angular
AngularAngular
Angular
 
Introduction to AngularJS - More Than Just Data Binding
Introduction to AngularJS - More Than Just Data BindingIntroduction to AngularJS - More Than Just Data Binding
Introduction to AngularJS - More Than Just Data Binding
 
Introduction of ASP.NET MVC and AngularJS
Introduction of ASP.NET MVC and AngularJSIntroduction of ASP.NET MVC and AngularJS
Introduction of ASP.NET MVC and AngularJS
 
What are the key distinctions between Angular and AngularJS?
What are the key distinctions between Angular and AngularJS?What are the key distinctions between Angular and AngularJS?
What are the key distinctions between Angular and AngularJS?
 
Getting started with angular js
Getting started with angular jsGetting started with angular js
Getting started with angular js
 
AngularJs (1.x) Presentation
AngularJs (1.x) PresentationAngularJs (1.x) Presentation
AngularJs (1.x) Presentation
 

Presentation_muffin_conference_2015