View
1.319
Download
1
Category
Tags:
Preview:
DESCRIPTION
Introduction à la programmation aspect, présentation générique des enjeux puis déclinaison en dotNet au travers de deux outils : Unity 2.1 et PostSharp
Citation preview
© Klee Group Février 2012 Prez Flash : Introduction AOP – Olivier GAUDEFROY 29
11
PrezFlash :: Introduction AOP
Auteur : Olivier GAUDEFROY
© Klee Group Février 2012 Prez Flash : Introduction AOP – Olivier GAUDEFROY 29
2
Binaire
Impératif
Procédural
OOPManaged OOP
Une courte histoire de la programmation – (1/2)
© Klee Group Février 2012 Prez Flash : Introduction AOP – Olivier GAUDEFROY 29
Pourquoi ces changements ?
3
Une courte histoire de la programmation – (2/2)
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 29
Un développement informatique industriel a nécessairement besoin d’adresser des besoins transverses.
4
Comment mutualiser ces comportements sans « salir » le code par une tonne d’abstraction ?
Quel est le problème de l’OOP ?
© Klee Group Février 2012 Prez Flash : Introduction AOP – Olivier GAUDEFROY 29
5
Méthode
Code métier
Transaction
Logging
Validation
Monitoring
Quel est le problème de l’OOP ?
© Klee Group Février 2012 Prez Flash : Introduction AOP – Olivier GAUDEFROY 29
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
6
© Klee Group Février 2012 Prez Flash : Introduction AOP – Olivier GAUDEFROY 29
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)
7
© Klee Group Février 2012 Prez Flash : Introduction AOP – Olivier GAUDEFROY 29
Un exemple simpliste …
8
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);}
Identification des cross cutting concerns
© Klee Group Février 2012 Prez Flash : Introduction AOP – Olivier GAUDEFROY 29
9
Un exemple simpliste …
public void LogToConsole(string method) { Console.WriteLine("Enter {0} Method", method);}
Ecriture du greffon de code (advice)
© Klee Group Février 2012 Prez Flash : Introduction AOP – Olivier GAUDEFROY 29
Un exemple simpliste …
10
public void Foo(List<object> items) { things.AddRange(items);} public void Bar(List<object> items) {
things.AddRange(items);}
Choisir les points d’action (pointcut)
Dans notre exemple :
POINTCUT = « Au début de chaque méthode »
© Klee Group Février 2012 Prez Flash : Introduction AOP – Olivier GAUDEFROY 29
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)
11
© Klee Group Février 2012 Prez Flash : Introduction AOP – Olivier GAUDEFROY 29
Code métier
Advices
OOP + AOP
12
Pointcut
© Klee Group Février 2012 Prez Flash : Introduction AOP – Olivier GAUDEFROY 29
Le tisseur de liens
13
TisseurTisseur
© Klee Group Février 2012 Prez Flash : Introduction AOP – Olivier GAUDEFROY 29
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)
14
© Klee Group Février 2012 Prez Flash : Introduction AOP – Olivier GAUDEFROY 29
Le tissage de liens – (2/2)
15
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 29
Passons à la pratique !
16
© Klee Group Février 2012 Prez Flash : Introduction AOP – Olivier GAUDEFROY 29
17
Tissage de lien dynamique
Démo
© Klee Group Février 2012 Prez Flash : Introduction AOP – Olivier GAUDEFROY 29
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
18
© Klee Group Février 2012 Prez Flash : Introduction AOP – Olivier GAUDEFROY 29
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
19
Références internes SIGC – Vinci Park
WebHFM - GMAO
© Klee Group Février 2012 Prez Flash : Introduction AOP – Olivier GAUDEFROY 29
C’est quoi un proxy d’exécution ?
20
Client Service
Client Service
Avant :
Après :
Proxy
© Klee Group Février 2012 Prez Flash : Introduction AOP – Olivier GAUDEFROY 29
Dynamic Proxies – Les capacités de Unity 2.0
21
Type Description
TransparentProxy Intercepteur d’instanceProxy créé par .Net Remoting
Hérite nécessairement de MarshalByRefObject
Interface Intercepteur d’instanceSupport d’une unique interface par objet.Génération de code dynamique (Reflection.Emit)
VirtualMethod Intercepteur de typeGé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 29
22
Unity Interception Pipeline
ClientApplication
Unity Interceptor
Unity Interception Pipeline
AdviceLog
AdviceCache Target Object
input return value
Invoke method
return value
© Klee Group Février 2012 Prez Flash : Introduction AOP – Olivier GAUDEFROY 29
23
Tissage de lien statique
Démo
© Klee Group Février 2012 Prez Flash : Introduction AOP – Olivier GAUDEFROY 29
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
24
© Klee Group Février 2012 Prez Flash : Introduction AOP – Olivier GAUDEFROY 29
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
Payant depuis la version 2.0
25
Références internes Facturation - Dalkia
© Klee Group Février 2012 Prez Flash : Introduction AOP – Olivier GAUDEFROY 29
Intégration dans le cycle de compilation – (1/2)
26
C#
Ressource
CSC.EXE
Assembly (.exe ou .dll)
Ressource
.netmodule
Cycle standard de compilation
© Klee Group Février 2012 Prez Flash : Introduction AOP – Olivier GAUDEFROY 29
Intégration dans le cycle de compilation – (2/2)
27
C#
Ressource
CSC.EXE Assembly Assembly
+AOP
AdvicesCycle de compilation modifié
© Klee Group Février 2012 Prez Flash : Introduction AOP – Olivier GAUDEFROY 29
Questions ?
Retrouvez nous sur le blog technique de Klee
http://blog.kleegroup.com/teknics
teKnics@kleegroup.com@teKnics_Klee
28
Recommended