inversion of control @ cd2008
DESCRIPTION
Inversion of Control @ CD2008TRANSCRIPT
![Page 1: Inversion of Control @ CD2008](https://reader033.vdocuments.us/reader033/viewer/2022061206/5482497cb4af9f636b8b47d7/html5/thumbnails/1.jpg)
Realizzare applicazioni estendibili e configurabili
Ambizioso?
Mauro Servienti
Microsoft MVP - Visual C#
Software Mason @ Managed Designs S.r.l.
![Page 2: Inversion of Control @ CD2008](https://reader033.vdocuments.us/reader033/viewer/2022061206/5482497cb4af9f636b8b47d7/html5/thumbnails/2.jpg)
Agenda
• Perchè?• Il “problema”;• Dogmi:
– Open Closed Principle;– Single Point of Responsability;
• Best Practice(s);• Inversion Of Control;• Chain Of Responsability;• Una voce fuori dal coro: System.AddIn;
![Page 3: Inversion of Control @ CD2008](https://reader033.vdocuments.us/reader033/viewer/2022061206/5482497cb4af9f636b8b47d7/html5/thumbnails/3.jpg)
• La bacchetta magica non esiste:– Ogni volta è una nuova avventura;– Non c’è una ricetta che vada bene per tutte le
salse ;-)– Esistono i Design Pattern....ma...
• Partire dai Pattern è rischioso:– Rischia di farci perdere la visione concreta del
progetto;– Refactoring to Pattern: “software mason”;
IMHO
![Page 4: Inversion of Control @ CD2008](https://reader033.vdocuments.us/reader033/viewer/2022061206/5482497cb4af9f636b8b47d7/html5/thumbnails/4.jpg)
ESTENSIBILITÀ... WHO CARES?
Perchè abbiamo bisogno di realizzare applicazioni estensibili.
![Page 5: Inversion of Control @ CD2008](https://reader033.vdocuments.us/reader033/viewer/2022061206/5482497cb4af9f636b8b47d7/html5/thumbnails/5.jpg)
• Supporto per l’integrazione e l’interoperabilità;
• Consente di modificare a caldo il comportamento;
• Elevatissima manutenibilità;• Verticalizzazione;• Testing;• Può essere un’ottimo “strumento”
commerciale;
Perchè?
![Page 6: Inversion of Control @ CD2008](https://reader033.vdocuments.us/reader033/viewer/2022061206/5482497cb4af9f636b8b47d7/html5/thumbnails/6.jpg)
LE DIPENDENZE STATICHEUn primo approccio al problema
![Page 7: Inversion of Control @ CD2008](https://reader033.vdocuments.us/reader033/viewer/2022061206/5482497cb4af9f636b8b47d7/html5/thumbnails/7.jpg)
il problema: le dipendenze statiche
Componente
ComAComponente
ComB
![Page 8: Inversion of Control @ CD2008](https://reader033.vdocuments.us/reader033/viewer/2022061206/5482497cb4af9f636b8b47d7/html5/thumbnails/8.jpg)
Componente
ComA
Componente
ComB
Interfaccia
IComB
Il cammino verso la soluzione...
...to be continued
![Page 9: Inversion of Control @ CD2008](https://reader033.vdocuments.us/reader033/viewer/2022061206/5482497cb4af9f636b8b47d7/html5/thumbnails/9.jpg)
I PRINCIPI GUIDAFobia da “dipendenza statica”?, un faro nella nebbia...
![Page 10: Inversion of Control @ CD2008](https://reader033.vdocuments.us/reader033/viewer/2022061206/5482497cb4af9f636b8b47d7/html5/thumbnails/10.jpg)
Open Closed Principle
“software entities should be open for extension, but closed for modification”
• Open for Extension:– Il componente deve essere estendibile;
• Closed for Modification:– L’estensione non deve portare ad una
rottura del rapporto;
![Page 11: Inversion of Control @ CD2008](https://reader033.vdocuments.us/reader033/viewer/2022061206/5482497cb4af9f636b8b47d7/html5/thumbnails/11.jpg)
Ma in soldoni?
• Quello che vogliamo evitare è che una semplice modifica in un punto si propaghi a macchi d’olio in tutta l’applicazione;– Eg: sostituibilità a “caldo” del DAL
• Se abbiamo un riferimento al DAL concreto non siamo “Closed”;
• Se facciamo assunzioni particolari non siamo “Closed”;
![Page 12: Inversion of Control @ CD2008](https://reader033.vdocuments.us/reader033/viewer/2022061206/5482497cb4af9f636b8b47d7/html5/thumbnails/12.jpg)
Non “Closed”
![Page 13: Inversion of Control @ CD2008](https://reader033.vdocuments.us/reader033/viewer/2022061206/5482497cb4af9f636b8b47d7/html5/thumbnails/13.jpg)
Single Point of Responsability
• Ad ognuno il suo ruolo;• Lo scope del ruolo assegnato deve
essere il più preciso possibile (focuse);
• Se ad un componente assegno più responsabilità violo l’Open Closed Principle;
![Page 14: Inversion of Control @ CD2008](https://reader033.vdocuments.us/reader033/viewer/2022061206/5482497cb4af9f636b8b47d7/html5/thumbnails/14.jpg)
Troppe responsabilità
![Page 15: Inversion of Control @ CD2008](https://reader033.vdocuments.us/reader033/viewer/2022061206/5482497cb4af9f636b8b47d7/html5/thumbnails/15.jpg)
BEST PRACTICE(S)Il panico da “Empty Solution”
![Page 16: Inversion of Control @ CD2008](https://reader033.vdocuments.us/reader033/viewer/2022061206/5482497cb4af9f636b8b47d7/html5/thumbnails/16.jpg)
Scrivere in ottica estensibilità
• Interfaces vs. Abstract classes;• Pensare agli entry point;• Prevedere quali dati potranno essere
necessari;• Le “pipeline”, un esempio da seguire:
– HttpModule, HttpHanlder;• Refactoring, refactoring, refactoring,
refactoring e ancora refactoring;
![Page 17: Inversion of Control @ CD2008](https://reader033.vdocuments.us/reader033/viewer/2022061206/5482497cb4af9f636b8b47d7/html5/thumbnails/17.jpg)
Interfaces vs. Abstract classes
Interface• Posso simulare
ereditarietà multipla;• Non “brucio” l’unico punto
di inheritance
Abstract Class• Mi permette di fornire
un’implementazione di base alle classi derivate;
• Colgo il meglio dei due mondi:• Interface verso il chiamante;• Abstract Class(es) nel modello;
Quindi?
![Page 18: Inversion of Control @ CD2008](https://reader033.vdocuments.us/reader033/viewer/2022061206/5482497cb4af9f636b8b47d7/html5/thumbnails/18.jpg)
Il meglio dei due mondi (1)
![Page 19: Inversion of Control @ CD2008](https://reader033.vdocuments.us/reader033/viewer/2022061206/5482497cb4af9f636b8b47d7/html5/thumbnails/19.jpg)
Il meglio dei due mondi (2)
...to be continued
![Page 20: Inversion of Control @ CD2008](https://reader033.vdocuments.us/reader033/viewer/2022061206/5482497cb4af9f636b8b47d7/html5/thumbnails/20.jpg)
Entry Point
• Dove avrò bisogno di estendere:– Ovunque ;-), mai chiudersi le porte...
• Un esempio: la “Conversazione”– è la Session? Si;– Perchè non usare la Session? E se
domani mattina la Session non mi andasse più bene?
– Mettiamo le mani avanti: “Facade”
![Page 21: Inversion of Control @ CD2008](https://reader033.vdocuments.us/reader033/viewer/2022061206/5482497cb4af9f636b8b47d7/html5/thumbnails/21.jpg)
Mascheriamo
![Page 22: Inversion of Control @ CD2008](https://reader033.vdocuments.us/reader033/viewer/2022061206/5482497cb4af9f636b8b47d7/html5/thumbnails/22.jpg)
...ma dietro le quinte?
• Slega il nostro modello da Asp.Net;– Siamo “Open”!;
• Quanto abbiamo investito?
![Page 23: Inversion of Control @ CD2008](https://reader033.vdocuments.us/reader033/viewer/2022061206/5482497cb4af9f636b8b47d7/html5/thumbnails/23.jpg)
Prevedere...
• Non abbiamo la sfera di cristallo• Incapsulare i dati in classi adatte al
loro trasporto: – EventArgs;– CancelEventArgs;– CustomEventArgs : estendiamo
CancelEventArgs/EventArgs
![Page 24: Inversion of Control @ CD2008](https://reader033.vdocuments.us/reader033/viewer/2022061206/5482497cb4af9f636b8b47d7/html5/thumbnails/24.jpg)
“Trasportare” i dati
• le informazioni non sono sufficienti
• ...non compila più: non siamo “Closed”
![Page 25: Inversion of Control @ CD2008](https://reader033.vdocuments.us/reader033/viewer/2022061206/5482497cb4af9f636b8b47d7/html5/thumbnails/25.jpg)
Le “pipeline”
• “pipeline”, un esempio da seguire:– HttpModule + HttpHanlder;
IProcessor
AIProcessor
BIProcessor
CIProcessor
n
Request
Data
![Page 26: Inversion of Control @ CD2008](https://reader033.vdocuments.us/reader033/viewer/2022061206/5482497cb4af9f636b8b47d7/html5/thumbnails/26.jpg)
Le “pipeline” (code)
![Page 27: Inversion of Control @ CD2008](https://reader033.vdocuments.us/reader033/viewer/2022061206/5482497cb4af9f636b8b47d7/html5/thumbnails/27.jpg)
Inversion of Control
Componente
ComA
Componente
ComB
Interfaccia
IComBServiceProvider(IoC Container)
IoCConfig
![Page 28: Inversion of Control @ CD2008](https://reader033.vdocuments.us/reader033/viewer/2022061206/5482497cb4af9f636b8b47d7/html5/thumbnails/28.jpg)
DEMOVediamolo in azione....
![Page 29: Inversion of Control @ CD2008](https://reader033.vdocuments.us/reader033/viewer/2022061206/5482497cb4af9f636b8b47d7/html5/thumbnails/29.jpg)
IoC Containers e non solo
• StructureMap;• Castle Windsor;• Spring.NET• Unity (Enterprise Library 4.0)
![Page 30: Inversion of Control @ CD2008](https://reader033.vdocuments.us/reader033/viewer/2022061206/5482497cb4af9f636b8b47d7/html5/thumbnails/30.jpg)
I’ve got the power <cit.>
• Se volessimo cambiare l’ordine...?• Se volessimo aggiungere/rimuovere
step...?• Se volessimo fare il tutto “a caldo”...?
![Page 31: Inversion of Control @ CD2008](https://reader033.vdocuments.us/reader033/viewer/2022061206/5482497cb4af9f636b8b47d7/html5/thumbnails/31.jpg)
DEMOVediamolo in azione....
![Page 32: Inversion of Control @ CD2008](https://reader033.vdocuments.us/reader033/viewer/2022061206/5482497cb4af9f636b8b47d7/html5/thumbnails/32.jpg)
IoC Containers: cosa offrono
• Contenitore di Servizi;• Lifecycle management;• Policy Injection (interceptors);
• Difetti? Si la gestione della configurazione...
![Page 33: Inversion of Control @ CD2008](https://reader033.vdocuments.us/reader033/viewer/2022061206/5482497cb4af9f636b8b47d7/html5/thumbnails/33.jpg)
DOMANDE?Non sparate sul pianista....
![Page 34: Inversion of Control @ CD2008](https://reader033.vdocuments.us/reader033/viewer/2022061206/5482497cb4af9f636b8b47d7/html5/thumbnails/34.jpg)
IL MODULO DI FEEDBACKGrazie a tutti, mi raccomando...