Weitere ähnliche Inhalte Ähnlich wie Introduction AOP (20) Introduction AOP1. PrezFlash :: Introduction AOP
Auteur : Olivier GAUDEFROY
© Klee Group Février 2012 Prez Flash : Introduction AOP – Olivier GAUDEFROY 1 29
2. Une courte histoire de la programmation – (1/2)
Managed
OOP OOP
Procédural
Impératif
Binaire
© Klee Group Février 2012 Prez Flash : Introduction AOP – Olivier GAUDEFROY 2 29
3. Une courte histoire de la programmation – (2/2)
Pourquoi ces changements ?
Changement de paradigme Raison
Binaire Impératif Parce qu’il le faut …
Imperatif Procédural Diviser l’application en plusieurs
éléments compréhensibles.
Procédural OOP Supprimer les contextes globaux,
mutualisation des comportements.
OOP Managed OOP Gestion automatisée de la
mémoire.
© Klee Group Février 2012 Prez Flash : Introduction AOP – Olivier GAUDEFROY 3 29
4. Quel est le problème de l’OOP ?
Un développement informatique industriel a nécessairement besoin
d’adresser des besoins transverses.
Comment mutualiser ces comportements sans « salir » le code par une
tonne d’abstraction ?
© Klee Group Février 2012 Prez Flash : Introduction AOP – Olivier GAUDEFROY 4 29
5. Quel est le problème de l’OOP ?
Méthode
Code métier
Transaction
Logging
Validation
Monitoring
© Klee Group Février 2012 Prez Flash : Introduction AOP – Olivier GAUDEFROY 5 29
6. Quel est le problème de l’OOP ?
L’OOP permet un bon niveau d’abstraction et une modularité fine
Pour adresser des problèmes complexes on arrive souvent au constat
suivant :
Multiplication des abstractions
Difficulté à décomposer
Couplage fort entre composants
Code spaghetti
Respect des principes de développement S.O.L.I.D
SOC Separation Of Concerns
SRP Single Responsibility Principle
© Klee Group Février 2012 Prez Flash : Introduction AOP – Olivier GAUDEFROY 6 29
7. Une brève définition de l’AOP
AOP = Aspect Oriented Programming
Paradigme de programmation
Le but : Isoler les besoins transverses qu’il est difficile de factoriser en OOP
Comment : Notion d’aspect transverse, complémentaire de la programmation
orientée objet
Le vocabulaire de l’AOP
Cross Cutting Concerns : les besoins transverses.
Advice : « greffon de code » permettant d’injecter le code nécessaire.
Pointcut : Localisation dans le code ou le greffon va être injecté.
Aspect : combinaison du greffon de code dans le point d’injection (Advice +
Pointcut)
© Klee Group Février 2012 Prez Flash : Introduction AOP – Olivier GAUDEFROY 7 29
8. Un exemple simpliste …
Identification des cross cutting concerns
public void Foo(List<object> items) {
Console.WriteLine("Enter Foo Method");
things.AddRange(items);
}
public void Bar(List<object> items) {
Console.WriteLine("Enter Bar Method");
things.AddRange(items);
}
© Klee Group Février 2012 Prez Flash : Introduction AOP – Olivier GAUDEFROY 8 29
9. Un exemple simpliste …
Ecriture du greffon de code (advice)
public void LogToConsole(string method) {
Console.WriteLine("Enter {0} Method", method);
}
© Klee Group Février 2012 Prez Flash : Introduction AOP – Olivier GAUDEFROY 9 29
10. Un exemple simpliste …
Choisir les points d’action (pointcut)
public void Foo(List<object> items) {
things.AddRange(items);
}
public void Bar(List<object> items) {
things.AddRange(items);
}
Dans notre exemple :
POINTCUT = « Au début de chaque méthode »
© Klee Group Février 2012 Prez Flash : Introduction AOP – Olivier GAUDEFROY 10 29
11. En résumé l’AOP consiste à
Identifier les besoins transverses (cross cutting concerns)
Ecrire un ou plusieurs greffons de code (advices)
Représentation de l’aspect
Encapsulation du comportement.
Choisir où appliquer ces greffons (pointcuts)
© Klee Group Février 2012 Prez Flash : Introduction AOP – Olivier GAUDEFROY 11 29
12. OOP + AOP
Code métier
Advices
Pointcut
© Klee Group Février 2012 Prez Flash : Introduction AOP – Olivier GAUDEFROY 12 29
13. Le tisseur de liens
Tisseur
© Klee Group Février 2012 Prez Flash : Introduction AOP – Olivier GAUDEFROY 13 29
14. Le tissage de liens – (1/2)
Le tissage (Weaving) : insertion de greffons de code dans le programme aux
points d’actions définis
Comment définir les points d’action ?
Via surcharge du compilateur
Via décoration (utilisation des meta-données)
Via configuration (XML, DSL, TXT)
Via programmation (API permettant de définir les pointcuts par code)
© Klee Group Février 2012 Prez Flash : Introduction AOP – Olivier GAUDEFROY 14 29
15. Le tissage de liens – (2/2)
On distingue trois grandes familles de tisseurs de liens :
Tissage de lien statique : lors de la phase de compilation (Compile-Time
Weaving)
Outils existant : Microsoft.CCI, Mono.Cecil, PostSharp
Outils à venir : Roslyn
Tissage de lien dynamique : à l’exécution du programme (Runtime Weaving)
Outils existants : Reflection.Emit, Unity, Ninject, Spring .net etc…
Tissage de lien hybride = Compile-Time Weaving + Runtime Weaving
Le type de tisseur utilisable est fortement dépendant de la technologie
© Klee Group Février 2012 Prez Flash : Introduction AOP – Olivier GAUDEFROY 15 29
16. Passons à la pratique !
© Klee Group Février 2012 Prez Flash : Introduction AOP – Olivier GAUDEFROY 16 29
17. Démo
© Klee Group Février 2012 Prez Flash : Introduction AOP – Olivier GAUDEFROY 17 29
18. Tissage dynamique - Contexte de la démo
Un programme simple
30 appels à la même méthode
Un appel unitaire coute 1s
Aspects à ajouter
Log
Cache
© Klee Group Février 2012 Prez Flash : Introduction AOP – Olivier GAUDEFROY 18 29
19. Présentation Unity 2.0
Microsoft Unity 2.0
Logiciel OpenSource
Pour le télécharger : http://unity.codeplex.com/
Qu’est-ce que c’est ?
Une librairie permettant l’injection de dépendances
Un framework léger d’AOP
Fonctions d’AOP
Tisseur de lien dynamique
Configuration mixte (API / XML)
Dynamic Proxies
Références internes
SIGC – Vinci Park
WebHFM - GMAO
© Klee Group Février 2012 Prez Flash : Introduction AOP – Olivier GAUDEFROY 19 29
20. C’est quoi un proxy d’exécution ?
Avant :
Client Service
Après :
Proxy
Client Service
© Klee Group Février 2012 Prez Flash : Introduction AOP – Olivier GAUDEFROY 20 29
21. Dynamic Proxies – Les capacités de Unity 2.0
Type Description
TransparentProxy Intercepteur d’instance
Proxy créé par .Net Remoting
Hérite nécessairement de MarshalByRefObject
Interface Intercepteur d’instance
Support d’une unique interface par objet.
Génération de code dynamique (Reflection.Emit)
VirtualMethod Intercepteur de type
Génération de code dynamique (Reflection.Emit)
Les méthodes doivent être marquées comme virtual
© Klee Group Février 2012 Prez Flash : Introduction AOP – Olivier GAUDEFROY 21 29
22. Unity Interception Pipeline
Client
Application
input return
value
Invoke method
Advice Advice
Unity
Log Cache Target Object
Interceptor
return value
Unity Interception Pipeline
© Klee Group Février 2012 Prez Flash : Introduction AOP – Olivier GAUDEFROY 22 29
23. Démo
© Klee Group Février 2012 Prez Flash : Introduction AOP – Olivier GAUDEFROY 23 29
24. Tissage statique - Contexte de la démo
Un programme simple
Une librairie de DTO
On souhaite connaitre la date d’instanciation de chaque objet
Aspects à ajouter
Implémentation dynamique d’une interface
Modification du constructeur
© Klee Group Février 2012 Prez Flash : Introduction AOP – Olivier GAUDEFROY 24 29
25. Présentation PostSharp
PostSharp
Projet OpenSource créé par Gaël Fraiteur
Fonctionnalités
Framework de tissage de lien statique
Infrastructure complète permettant d’être très productif
Avantages
Intégration dans MSBuild
Intégration dans Visual Studio
Références internes
Facturation - Dalkia
Payant depuis la version 2.0
© Klee Group Février 2012 Prez Flash : Introduction AOP – Olivier GAUDEFROY 25 29
26. Intégration dans le cycle de compilation – (1/2)
Cycle standard de compilation
Assembly
(.exe ou .dll)
C#
CSC.EXE .netmodule
Ressource
Ressource
© Klee Group Février 2012 Prez Flash : Introduction AOP – Olivier GAUDEFROY 26 29
27. Intégration dans le cycle de compilation – (2/2)
Advices
Cycle de compilation modifié
C#
Assembly
CSC.EXE Assembly +
AOP
Ressource
© Klee Group Février 2012 Prez Flash : Introduction AOP – Olivier GAUDEFROY 27 29
28. Questions ?
Retrouvez nous sur le blog technique de Klee
http://blog.kleegroup.com/teknics
teKnics@kleegroup.com
@teKnics_Klee
© Klee Group Février 2012 Prez Flash : Introduction AOP – Olivier GAUDEFROY 28 29