SlideShare a Scribd company logo
1 of 29
Download to read offline
ANGULAR
IL WEB FRAMEWORK PIÙ STABILE SUL MERCATO
Valerio Como
Made in 74017
Valerio Como
Software Developer
Valerio Como
Membro attivo dal giorno 0
Agenda - Servizi
Servizi
Cosa sono e perchè.
Dependency Injection
Implementazione in Angular
Challenge - Servizi
Challenge - DI
Big picture
Servizi
Servizi
Logica di business
Cosa sono e perchè.
DRY
Implementazione in Angular
Unit test
Effettuare chiamate HTTP con Angular
Servizi
ng generate service
nome-del-servizio
Genera il file
nome-del-servizio.service.
ts
@Injectable()
Metadati per Dependency
Injection
import { Injectable } from '@angular/core' ;
@Injectable()
export class LoggerService {
constructor() { }
}
Dependency
Injection
Dependency Injection
Design Pattern
Disaccoppiamento tra oggetti
Interfacce
Inversion of Control (IoC)
Perchè DI?
Perchè DI?
Test
Difficile da testare
export class Car {
engine: Engine;
steering: SteeringWheel;
constructor() {
this.engine = new Engine();
this.steering = new SteeringWheel();
}
powerOn() {
this.engine.powerOn();
}
}
Perchè DI?
Test
Difficile da testare
Fragile
Non aperta al
cambiamento
export class Car {
engine: Engine;
steering: SteeringWheel;
constructor() {
this.engine = new Engine();
this.steering = new SteeringWheel();
}
powerOn() {
this.engine.powerOn();
}
}
Perchè DI?
Test
Difficile da testare
Fragile
Non aperta al
cambiamento
Non flessibile
Legata alla
implementazione
export class Car {
engine: Engine;
steering: SteeringWheel;
constructor() {
this.engine = new Engine();
this.steering = new SteeringWheel();
}
powerOn() {
this.engine.powerOn();
}
}
Perchè DI?
Dipendenze nel
costruttore
Passo le dipendenze
Test
Facile da testare
Estendibile
interface al posto di classi
concrete
export class Car {
constructor(
public engine: Engine,
public steering: SteeringWheel
) { }
powerOn() {
this.engine.powerOn();
}
}
Perchè DI?
Dipendenze nel
costruttore
Passo le dipendenze
Test
Facile da testare
Estendibile
interface al posto di classi
concrete
export class Car {
constructor(
public engine: Engine,
public steering: SteeringWheel
) { }
powerOn() {
this.engine.powerOn();
}
}
let car = new Car(new Engine(), new SteeringWheel());
Perchè DI?
Factory pattern
Crea istanze di Car
export class CarFactory {
createCar() {
return new Car(
this.createEngine(),
this.createSteeringWheel()
);
}
createEngine() {
return new Engine();
}
createSteeringWheel() {
return new SteeringWheel();
}
}
let car = new CarFactory().createCar();
Perchè DI?
Factory pattern
Crea istanze di Car
Code Smell
La classe factory può
evolvere velocemente
export class CarFactory {
createCar() {
return new Car(
this.createEngine(),
this.createSteeringWheel()
);
}
createEngine() {
return new Engine();
}
createSteeringWheel() {
return new SteeringWheel();
}
}
let car = new CarFactory().createCar();
Perchè DI?
Factory pattern
Crea istanze di Car
Code Smell
La classe factory può
evolvere velocemente
DI
Chiedo al container una
istanza di Car
let car = container.get(Car);
DI in Angular
Dependency Injection
Dependency Injection Gerarchica
Tree model, Injector bubbling
Service Provider
Component vs ngModule
Singleton
Una istanza per scope
Testabilità
Dependency
Injection
Creare un service
import { Injectable } from '@angular/core';
@Injectable()
export class LoggerService {
constructor() { }
log(msg: any) { console.log(msg); }
error(msg: any) { console.error(msg); }
warn(msg: any) { console.warn(msg); }
}
Dependency
Injection
Creare un service
Registrare un service
…
providers: [LoggerService]
…
Dependency
Injection
Creare un service
Registrare un service
Ingettare il service
…
constructor(private logger: LoggerService) { }
…
Challenges
Challenge - Servizi
● Andiamo su https://stackblitz.com/edit/aw-services-challenge
● Creare un nuovo servizio con nome Api nella cartella app. Il servizio dovrà implementare le chiamate
alle API per otterene i dati e effettuare l’upvote.
● Importare il database nel servizio
● Definire il metodo pubblico getLinks(page: number) nel servizio creato. Dovrà restituire i Link dal
database
● Rimuovere DATABASE da link-list.component.ts, istanziare il servizio e utilizzare il metodo getLinks
● Definire il metodo pubblico upvote(id: number) nel servizio. Dovrà implementare la logica di upvoted,
definita in link-list.component.ts
● Istanziare il servizio e utilizzare il metodo upvote, ridefinendo l'implementazione di upvote in
link-item.component.ts. Eliminare la proprietà upvoted di tipo EventEmitter in questo componente
Challenge - DI
● Andiamo su https://stackblitz.com/edit/aw-di-challenge
● Aggiungere il provider per ApiService in app.module.ts
● Iniettare il servizio ApiService nel costruttore dei componenti che lo utilizzano. Rimuovere
l'istanziazione di ApiService, sostituendola con il servizio iniettato
● Creare un nuovo servizio con nome Logger
● Implementare in LoggerService la proprietà privata counter
● Implementare in LoggerService le seguenti funzioni. Ogni funzione deve incrementare la proprietà
counter, stampare il valore di counter e del messaggio
○ log(e)
○ warn(e)
○ error(e)
● Definire il provider di LoggerService nel decoratore @NgModule
● Iniettare il servizio LoggerService nel costruttore dei componenti
● Utilizzare la funzione log di LoggerService nei metodi changePage e upvote definiti nei component
Challenge - DI
● Sperimenta! Definire il provider di LoggerService nel decoratore @Component in uno due
componenti. Cosa accade?

More Related Content

Similar to Angular: Servizi e DI

Entity Framework 4 vs NHibernate 3
Entity Framework 4 vs NHibernate 3Entity Framework 4 vs NHibernate 3
Entity Framework 4 vs NHibernate 3Martino Bordin
 
ASP.NET MVC: Andare oltre il 100% (Web@work)
ASP.NET MVC: Andare oltre il 100% (Web@work)ASP.NET MVC: Andare oltre il 100% (Web@work)
ASP.NET MVC: Andare oltre il 100% (Web@work)Giorgio Di Nardo
 
Meetup DotNetCode Settembre 2018 - ASP.NET Core 2.1
Meetup DotNetCode Settembre 2018 - ASP.NET Core 2.1Meetup DotNetCode Settembre 2018 - ASP.NET Core 2.1
Meetup DotNetCode Settembre 2018 - ASP.NET Core 2.1dotnetcode
 
Meetup ASP.NET Core Angular
Meetup ASP.NET Core AngularMeetup ASP.NET Core Angular
Meetup ASP.NET Core Angulardotnetcode
 
Modi innovativi per costruire App
Modi innovativi per costruire AppModi innovativi per costruire App
Modi innovativi per costruire AppCommit University
 
Asp.net 4 Community Tour VS2010
Asp.net 4 Community Tour VS2010Asp.net 4 Community Tour VS2010
Asp.net 4 Community Tour VS2010Fabrizio Bernabei
 
Cert03 70-486 developing asp.net mvc 4 web applications
Cert03   70-486 developing asp.net mvc 4 web applicationsCert03   70-486 developing asp.net mvc 4 web applications
Cert03 70-486 developing asp.net mvc 4 web applicationsDotNetCampus
 
Alessandro Forte - MVP vs MVC
Alessandro Forte - MVP vs MVCAlessandro Forte - MVP vs MVC
Alessandro Forte - MVP vs MVCAlessandro Forte
 
.Net 4.0 Preview @ UGIdotNet
.Net 4.0 Preview @ UGIdotNet.Net 4.0 Preview @ UGIdotNet
.Net 4.0 Preview @ UGIdotNetMauro Servienti
 
ASP.NET MVC 6 - uno sguardo al futuro
ASP.NET MVC 6 - uno sguardo al futuroASP.NET MVC 6 - uno sguardo al futuro
ASP.NET MVC 6 - uno sguardo al futuroAndrea Dottor
 
Asp.Net MVC 2 :: VS 2010 Community Tour
Asp.Net MVC 2 :: VS 2010 Community TourAsp.Net MVC 2 :: VS 2010 Community Tour
Asp.Net MVC 2 :: VS 2010 Community TourAndrea Balducci
 
Presentazione: uno studio sull'efficacia di checker automatici per la moderni...
Presentazione: uno studio sull'efficacia di checker automatici per la moderni...Presentazione: uno studio sull'efficacia di checker automatici per la moderni...
Presentazione: uno studio sull'efficacia di checker automatici per la moderni...Idriss Riouak
 
Application insights - Meet{cast} - Meetup #AperiTech dotNET{podcast} - Marzo...
Application insights - Meet{cast} - Meetup #AperiTech dotNET{podcast} - Marzo...Application insights - Meet{cast} - Meetup #AperiTech dotNET{podcast} - Marzo...
Application insights - Meet{cast} - Meetup #AperiTech dotNET{podcast} - Marzo...Roberto Albano
 
Webkit meets native development
Webkit meets native developmentWebkit meets native development
Webkit meets native developmentNicholas Valbusa
 
Introduzioni ai services in Angular 4 e ad RxJS
Introduzioni ai services in Angular 4 e ad RxJSIntroduzioni ai services in Angular 4 e ad RxJS
Introduzioni ai services in Angular 4 e ad RxJSGiovanni Buffa
 

Similar to Angular: Servizi e DI (20)

Entity Framework 4 vs NHibernate 3
Entity Framework 4 vs NHibernate 3Entity Framework 4 vs NHibernate 3
Entity Framework 4 vs NHibernate 3
 
ASP.NET MVC: Andare oltre il 100% (Web@work)
ASP.NET MVC: Andare oltre il 100% (Web@work)ASP.NET MVC: Andare oltre il 100% (Web@work)
ASP.NET MVC: Andare oltre il 100% (Web@work)
 
Meetup DotNetCode Settembre 2018 - ASP.NET Core 2.1
Meetup DotNetCode Settembre 2018 - ASP.NET Core 2.1Meetup DotNetCode Settembre 2018 - ASP.NET Core 2.1
Meetup DotNetCode Settembre 2018 - ASP.NET Core 2.1
 
Meetup ASP.NET Core Angular
Meetup ASP.NET Core AngularMeetup ASP.NET Core Angular
Meetup ASP.NET Core Angular
 
Modi innovativi per costruire App
Modi innovativi per costruire AppModi innovativi per costruire App
Modi innovativi per costruire App
 
Spring 2.5
Spring 2.5Spring 2.5
Spring 2.5
 
Swagger pertutti
Swagger pertuttiSwagger pertutti
Swagger pertutti
 
Asp.net 4 Community Tour VS2010
Asp.net 4 Community Tour VS2010Asp.net 4 Community Tour VS2010
Asp.net 4 Community Tour VS2010
 
Cert03 70-486 developing asp.net mvc 4 web applications
Cert03   70-486 developing asp.net mvc 4 web applicationsCert03   70-486 developing asp.net mvc 4 web applications
Cert03 70-486 developing asp.net mvc 4 web applications
 
#dd12 Applicazioni a tre voci (Android e Domino)
#dd12 Applicazioni a tre voci (Android e Domino)#dd12 Applicazioni a tre voci (Android e Domino)
#dd12 Applicazioni a tre voci (Android e Domino)
 
Alessandro Forte - MVP vs MVC
Alessandro Forte - MVP vs MVCAlessandro Forte - MVP vs MVC
Alessandro Forte - MVP vs MVC
 
.Net 4.0 Preview @ UGIdotNet
.Net 4.0 Preview @ UGIdotNet.Net 4.0 Preview @ UGIdotNet
.Net 4.0 Preview @ UGIdotNet
 
ASP.NET MVC 6 - uno sguardo al futuro
ASP.NET MVC 6 - uno sguardo al futuroASP.NET MVC 6 - uno sguardo al futuro
ASP.NET MVC 6 - uno sguardo al futuro
 
Asp.Net MVC 2 :: VS 2010 Community Tour
Asp.Net MVC 2 :: VS 2010 Community TourAsp.Net MVC 2 :: VS 2010 Community Tour
Asp.Net MVC 2 :: VS 2010 Community Tour
 
Presentazione: uno studio sull'efficacia di checker automatici per la moderni...
Presentazione: uno studio sull'efficacia di checker automatici per la moderni...Presentazione: uno studio sull'efficacia di checker automatici per la moderni...
Presentazione: uno studio sull'efficacia di checker automatici per la moderni...
 
Application insights - Meet{cast} - Meetup #AperiTech dotNET{podcast} - Marzo...
Application insights - Meet{cast} - Meetup #AperiTech dotNET{podcast} - Marzo...Application insights - Meet{cast} - Meetup #AperiTech dotNET{podcast} - Marzo...
Application insights - Meet{cast} - Meetup #AperiTech dotNET{podcast} - Marzo...
 
Webkit meets native development
Webkit meets native developmentWebkit meets native development
Webkit meets native development
 
Introduzioni ai services in Angular 4 e ad RxJS
Introduzioni ai services in Angular 4 e ad RxJSIntroduzioni ai services in Angular 4 e ad RxJS
Introduzioni ai services in Angular 4 e ad RxJS
 
Notes for an Enterprise Scheduling Distributed Application
Notes for an Enterprise Scheduling Distributed ApplicationNotes for an Enterprise Scheduling Distributed Application
Notes for an Enterprise Scheduling Distributed Application
 
Hexagonal architecture ita
Hexagonal architecture itaHexagonal architecture ita
Hexagonal architecture ita
 

More from Valerio Como

Deepfake: profondo falso
Deepfake: profondo falsoDeepfake: profondo falso
Deepfake: profondo falsoValerio Como
 
Introduzione al software libero
Introduzione al software liberoIntroduzione al software libero
Introduzione al software liberoValerio Como
 
Risoluzione DNS per IPv6: sperimentazione nella rete del dominio uniba.it
Risoluzione DNS per IPv6: sperimentazione nella rete del dominio uniba.itRisoluzione DNS per IPv6: sperimentazione nella rete del dominio uniba.it
Risoluzione DNS per IPv6: sperimentazione nella rete del dominio uniba.itValerio Como
 
Autenticazione nei sistemi distribuiti: da Kerberos ai sistemi service oriented
Autenticazione nei sistemi distribuiti: da Kerberos ai sistemi service orientedAutenticazione nei sistemi distribuiti: da Kerberos ai sistemi service oriented
Autenticazione nei sistemi distribuiti: da Kerberos ai sistemi service orientedValerio Como
 

More from Valerio Como (7)

Deepfake: profondo falso
Deepfake: profondo falsoDeepfake: profondo falso
Deepfake: profondo falso
 
Introduzione al software libero
Introduzione al software liberoIntroduzione al software libero
Introduzione al software libero
 
Web Assembly
Web AssemblyWeb Assembly
Web Assembly
 
Yeoman generator
Yeoman generatorYeoman generator
Yeoman generator
 
Git flow
Git flowGit flow
Git flow
 
Risoluzione DNS per IPv6: sperimentazione nella rete del dominio uniba.it
Risoluzione DNS per IPv6: sperimentazione nella rete del dominio uniba.itRisoluzione DNS per IPv6: sperimentazione nella rete del dominio uniba.it
Risoluzione DNS per IPv6: sperimentazione nella rete del dominio uniba.it
 
Autenticazione nei sistemi distribuiti: da Kerberos ai sistemi service oriented
Autenticazione nei sistemi distribuiti: da Kerberos ai sistemi service orientedAutenticazione nei sistemi distribuiti: da Kerberos ai sistemi service oriented
Autenticazione nei sistemi distribuiti: da Kerberos ai sistemi service oriented
 

Angular: Servizi e DI

  • 1. ANGULAR IL WEB FRAMEWORK PIÙ STABILE SUL MERCATO
  • 5. Agenda - Servizi Servizi Cosa sono e perchè. Dependency Injection Implementazione in Angular Challenge - Servizi Challenge - DI
  • 8. Servizi Logica di business Cosa sono e perchè. DRY Implementazione in Angular Unit test Effettuare chiamate HTTP con Angular
  • 9. Servizi ng generate service nome-del-servizio Genera il file nome-del-servizio.service. ts @Injectable() Metadati per Dependency Injection import { Injectable } from '@angular/core' ; @Injectable() export class LoggerService { constructor() { } }
  • 11. Dependency Injection Design Pattern Disaccoppiamento tra oggetti Interfacce Inversion of Control (IoC)
  • 13. Perchè DI? Test Difficile da testare export class Car { engine: Engine; steering: SteeringWheel; constructor() { this.engine = new Engine(); this.steering = new SteeringWheel(); } powerOn() { this.engine.powerOn(); } }
  • 14. Perchè DI? Test Difficile da testare Fragile Non aperta al cambiamento export class Car { engine: Engine; steering: SteeringWheel; constructor() { this.engine = new Engine(); this.steering = new SteeringWheel(); } powerOn() { this.engine.powerOn(); } }
  • 15. Perchè DI? Test Difficile da testare Fragile Non aperta al cambiamento Non flessibile Legata alla implementazione export class Car { engine: Engine; steering: SteeringWheel; constructor() { this.engine = new Engine(); this.steering = new SteeringWheel(); } powerOn() { this.engine.powerOn(); } }
  • 16. Perchè DI? Dipendenze nel costruttore Passo le dipendenze Test Facile da testare Estendibile interface al posto di classi concrete export class Car { constructor( public engine: Engine, public steering: SteeringWheel ) { } powerOn() { this.engine.powerOn(); } }
  • 17. Perchè DI? Dipendenze nel costruttore Passo le dipendenze Test Facile da testare Estendibile interface al posto di classi concrete export class Car { constructor( public engine: Engine, public steering: SteeringWheel ) { } powerOn() { this.engine.powerOn(); } } let car = new Car(new Engine(), new SteeringWheel());
  • 18. Perchè DI? Factory pattern Crea istanze di Car export class CarFactory { createCar() { return new Car( this.createEngine(), this.createSteeringWheel() ); } createEngine() { return new Engine(); } createSteeringWheel() { return new SteeringWheel(); } } let car = new CarFactory().createCar();
  • 19. Perchè DI? Factory pattern Crea istanze di Car Code Smell La classe factory può evolvere velocemente export class CarFactory { createCar() { return new Car( this.createEngine(), this.createSteeringWheel() ); } createEngine() { return new Engine(); } createSteeringWheel() { return new SteeringWheel(); } } let car = new CarFactory().createCar();
  • 20. Perchè DI? Factory pattern Crea istanze di Car Code Smell La classe factory può evolvere velocemente DI Chiedo al container una istanza di Car let car = container.get(Car);
  • 22. Dependency Injection Dependency Injection Gerarchica Tree model, Injector bubbling Service Provider Component vs ngModule Singleton Una istanza per scope Testabilità
  • 23. Dependency Injection Creare un service import { Injectable } from '@angular/core'; @Injectable() export class LoggerService { constructor() { } log(msg: any) { console.log(msg); } error(msg: any) { console.error(msg); } warn(msg: any) { console.warn(msg); } }
  • 24. Dependency Injection Creare un service Registrare un service … providers: [LoggerService] …
  • 25. Dependency Injection Creare un service Registrare un service Ingettare il service … constructor(private logger: LoggerService) { } …
  • 27. Challenge - Servizi ● Andiamo su https://stackblitz.com/edit/aw-services-challenge ● Creare un nuovo servizio con nome Api nella cartella app. Il servizio dovrà implementare le chiamate alle API per otterene i dati e effettuare l’upvote. ● Importare il database nel servizio ● Definire il metodo pubblico getLinks(page: number) nel servizio creato. Dovrà restituire i Link dal database ● Rimuovere DATABASE da link-list.component.ts, istanziare il servizio e utilizzare il metodo getLinks ● Definire il metodo pubblico upvote(id: number) nel servizio. Dovrà implementare la logica di upvoted, definita in link-list.component.ts ● Istanziare il servizio e utilizzare il metodo upvote, ridefinendo l'implementazione di upvote in link-item.component.ts. Eliminare la proprietà upvoted di tipo EventEmitter in questo componente
  • 28. Challenge - DI ● Andiamo su https://stackblitz.com/edit/aw-di-challenge ● Aggiungere il provider per ApiService in app.module.ts ● Iniettare il servizio ApiService nel costruttore dei componenti che lo utilizzano. Rimuovere l'istanziazione di ApiService, sostituendola con il servizio iniettato ● Creare un nuovo servizio con nome Logger ● Implementare in LoggerService la proprietà privata counter ● Implementare in LoggerService le seguenti funzioni. Ogni funzione deve incrementare la proprietà counter, stampare il valore di counter e del messaggio ○ log(e) ○ warn(e) ○ error(e) ● Definire il provider di LoggerService nel decoratore @NgModule ● Iniettare il servizio LoggerService nel costruttore dei componenti ● Utilizzare la funzione log di LoggerService nei metodi changePage e upvote definiti nei component
  • 29. Challenge - DI ● Sperimenta! Definire il provider di LoggerService nel decoratore @Component in uno due componenti. Cosa accade?