net uy meetup 4 - aop & postsharp by bruno bologna & fabian fernandez
DESCRIPTION
TRANSCRIPT
![Page 1: NET UY Meetup 4 - AOP & PostSharp by Bruno Bologna & Fabian Fernandez](https://reader035.vdocuments.us/reader035/viewer/2022070302/547eef7eb4af9f772b8b4674/html5/thumbnails/1.jpg)
AOP&
@bolognabruno@fabifernandez23
![Page 2: NET UY Meetup 4 - AOP & PostSharp by Bruno Bologna & Fabian Fernandez](https://reader035.vdocuments.us/reader035/viewer/2022070302/547eef7eb4af9f772b8b4674/html5/thumbnails/2.jpg)
Agenda
• ¿Que es el mal diseño?• ¿Que es AOP?• ¿Como funciona AOP?• Tipos AOP• Postsharp• Vida de un aspecto• Out of the Box Aspects• Consideraciones con Postsharp
![Page 3: NET UY Meetup 4 - AOP & PostSharp by Bruno Bologna & Fabian Fernandez](https://reader035.vdocuments.us/reader035/viewer/2022070302/547eef7eb4af9f772b8b4674/html5/thumbnails/3.jpg)
¿Que es el mal diseño?
• Recordemos momentos que nos vimos enfrentados a código mal diseñado.
• Analicemos que características hacen que el código no sea bueno.
![Page 4: NET UY Meetup 4 - AOP & PostSharp by Bruno Bologna & Fabian Fernandez](https://reader035.vdocuments.us/reader035/viewer/2022070302/547eef7eb4af9f772b8b4674/html5/thumbnails/4.jpg)
¿Que es el mal diseño?
![Page 5: NET UY Meetup 4 - AOP & PostSharp by Bruno Bologna & Fabian Fernandez](https://reader035.vdocuments.us/reader035/viewer/2022070302/547eef7eb4af9f772b8b4674/html5/thumbnails/5.jpg)
Dificultades de un mal diseño
• Muy difícil modificar código sin introducir nuevos bugs.
• Por más que se corrijan bugs, el mal diseño se mantiene y cada vez tiende a ser peor
![Page 6: NET UY Meetup 4 - AOP & PostSharp by Bruno Bologna & Fabian Fernandez](https://reader035.vdocuments.us/reader035/viewer/2022070302/547eef7eb4af9f772b8b4674/html5/thumbnails/6.jpg)
Arreglar algo mal diseñado...
![Page 7: NET UY Meetup 4 - AOP & PostSharp by Bruno Bologna & Fabian Fernandez](https://reader035.vdocuments.us/reader035/viewer/2022070302/547eef7eb4af9f772b8b4674/html5/thumbnails/7.jpg)
Ejemplo de código - MeetupApp
![Page 8: NET UY Meetup 4 - AOP & PostSharp by Bruno Bologna & Fabian Fernandez](https://reader035.vdocuments.us/reader035/viewer/2022070302/547eef7eb4af9f772b8b4674/html5/thumbnails/8.jpg)
Un pequeño análisis
• Por cada bloque de código que queremos encerrar en try/catch estamos utilizando al menos 8 líneas más de código.
![Page 9: NET UY Meetup 4 - AOP & PostSharp by Bruno Bologna & Fabian Fernandez](https://reader035.vdocuments.us/reader035/viewer/2022070302/547eef7eb4af9f772b8b4674/html5/thumbnails/9.jpg)
A la hora de crecer...
• 10 métodos que encierran código en bloques try/catch representarían al menos 80 líneas de código repetido
![Page 10: NET UY Meetup 4 - AOP & PostSharp by Bruno Bologna & Fabian Fernandez](https://reader035.vdocuments.us/reader035/viewer/2022070302/547eef7eb4af9f772b8b4674/html5/thumbnails/10.jpg)
Esto no es nuevo...
• Un estudio publicado por IEEE TRANSACTIONS ON SOFTWARE ENGINEERING demostró que la cantidad de defectos en una funcionalidad están relacionadas en dos factores:
1. La cantidad de lineas de código que componen dicha funcionalidad .
2. La repetición de dicha funcionalidad a traves de varios artefactos de código.
![Page 11: NET UY Meetup 4 - AOP & PostSharp by Bruno Bologna & Fabian Fernandez](https://reader035.vdocuments.us/reader035/viewer/2022070302/547eef7eb4af9f772b8b4674/html5/thumbnails/11.jpg)
Ademas...
• También demostró que las responsabilidades "cross-cutting" repetidas en la aplicación suelen tener más defectos que las clásicas funcionalidades de negocio.
![Page 12: NET UY Meetup 4 - AOP & PostSharp by Bruno Bologna & Fabian Fernandez](https://reader035.vdocuments.us/reader035/viewer/2022070302/547eef7eb4af9f772b8b4674/html5/thumbnails/12.jpg)
¿Que es AOP?
• AOP es una técnica de desarrollo de software que apunta a incrementar la modularidad permitiendo separación de responsabilidades "cross-cutting".
• Esta técnica complementa y no remplaza el paradigma actual de programación orientada a objetos.
![Page 13: NET UY Meetup 4 - AOP & PostSharp by Bruno Bologna & Fabian Fernandez](https://reader035.vdocuments.us/reader035/viewer/2022070302/547eef7eb4af9f772b8b4674/html5/thumbnails/13.jpg)
¿Que es AOP?
• Con AOP podemos definir funcionalidad "cross-cutting" en un único lugar y aplicarla declarativamente donde sea necesario sin modificar la clase donde aplica esta funcionalidad.
![Page 14: NET UY Meetup 4 - AOP & PostSharp by Bruno Bologna & Fabian Fernandez](https://reader035.vdocuments.us/reader035/viewer/2022070302/547eef7eb4af9f772b8b4674/html5/thumbnails/14.jpg)
Responsabilidades cross-cutting
• Logging• Auditoria• Transacciones• Locking & thread dispatching• Caching• Exception handling
![Page 15: NET UY Meetup 4 - AOP & PostSharp by Bruno Bologna & Fabian Fernandez](https://reader035.vdocuments.us/reader035/viewer/2022070302/547eef7eb4af9f772b8b4674/html5/thumbnails/15.jpg)
Principios que cumple AOP
• DRY (Don't Repeat Yourself)• Principios SOLID• Single Responsibility• OPEN/CLOSE
![Page 16: NET UY Meetup 4 - AOP & PostSharp by Bruno Bologna & Fabian Fernandez](https://reader035.vdocuments.us/reader035/viewer/2022070302/547eef7eb4af9f772b8b4674/html5/thumbnails/16.jpg)
¿Como funciona AOP?
![Page 17: NET UY Meetup 4 - AOP & PostSharp by Bruno Bologna & Fabian Fernandez](https://reader035.vdocuments.us/reader035/viewer/2022070302/547eef7eb4af9f772b8b4674/html5/thumbnails/17.jpg)
Identificamos responsabilidades Cross-Cutting
![Page 18: NET UY Meetup 4 - AOP & PostSharp by Bruno Bologna & Fabian Fernandez](https://reader035.vdocuments.us/reader035/viewer/2022070302/547eef7eb4af9f772b8b4674/html5/thumbnails/18.jpg)
Creamos un aspecto para dicha responsabilidad
![Page 19: NET UY Meetup 4 - AOP & PostSharp by Bruno Bologna & Fabian Fernandez](https://reader035.vdocuments.us/reader035/viewer/2022070302/547eef7eb4af9f772b8b4674/html5/thumbnails/19.jpg)
Aplicamos el aspecto a nuestras clases
![Page 20: NET UY Meetup 4 - AOP & PostSharp by Bruno Bologna & Fabian Fernandez](https://reader035.vdocuments.us/reader035/viewer/2022070302/547eef7eb4af9f772b8b4674/html5/thumbnails/20.jpg)
Tipos AOP
Interceptors• Usa el patrón DynamicProxy• Se resuelve en tiempo de ejecuciónIL Code Weaving• Inyecta código en tiempo de compilación• No usan DynamicProxy
![Page 21: NET UY Meetup 4 - AOP & PostSharp by Bruno Bologna & Fabian Fernandez](https://reader035.vdocuments.us/reader035/viewer/2022070302/547eef7eb4af9f772b8b4674/html5/thumbnails/21.jpg)
Interceptors
![Page 22: NET UY Meetup 4 - AOP & PostSharp by Bruno Bologna & Fabian Fernandez](https://reader035.vdocuments.us/reader035/viewer/2022070302/547eef7eb4af9f772b8b4674/html5/thumbnails/22.jpg)
IL Code Weaving
![Page 23: NET UY Meetup 4 - AOP & PostSharp by Bruno Bologna & Fabian Fernandez](https://reader035.vdocuments.us/reader035/viewer/2022070302/547eef7eb4af9f772b8b4674/html5/thumbnails/23.jpg)
Comparación de frameworks
![Page 24: NET UY Meetup 4 - AOP & PostSharp by Bruno Bologna & Fabian Fernandez](https://reader035.vdocuments.us/reader035/viewer/2022070302/547eef7eb4af9f772b8b4674/html5/thumbnails/24.jpg)
Features de cada framework
![Page 25: NET UY Meetup 4 - AOP & PostSharp by Bruno Bologna & Fabian Fernandez](https://reader035.vdocuments.us/reader035/viewer/2022070302/547eef7eb4af9f772b8b4674/html5/thumbnails/25.jpg)
De que forma podemos aplicar aspectos
![Page 26: NET UY Meetup 4 - AOP & PostSharp by Bruno Bologna & Fabian Fernandez](https://reader035.vdocuments.us/reader035/viewer/2022070302/547eef7eb4af9f772b8b4674/html5/thumbnails/26.jpg)
Otros puntos importantes
![Page 27: NET UY Meetup 4 - AOP & PostSharp by Bruno Bologna & Fabian Fernandez](https://reader035.vdocuments.us/reader035/viewer/2022070302/547eef7eb4af9f772b8b4674/html5/thumbnails/27.jpg)
![Page 28: NET UY Meetup 4 - AOP & PostSharp by Bruno Bologna & Fabian Fernandez](https://reader035.vdocuments.us/reader035/viewer/2022070302/547eef7eb4af9f772b8b4674/html5/thumbnails/28.jpg)
http://postsharp.net
![Page 29: NET UY Meetup 4 - AOP & PostSharp by Bruno Bologna & Fabian Fernandez](https://reader035.vdocuments.us/reader035/viewer/2022070302/547eef7eb4af9f772b8b4674/html5/thumbnails/29.jpg)
Vida de un Aspecto
• Compila tu assembly• Entra PostSharp a jugar:– Se instancia el aspecto para el target code– CompileTimeValidation– CompileTimeInitialization– Serialización por target code• Managed resource
– Binary stream (default)– XML– MSIL
CompileTime
![Page 30: NET UY Meetup 4 - AOP & PostSharp by Bruno Bologna & Fabian Fernandez](https://reader035.vdocuments.us/reader035/viewer/2022070302/547eef7eb4af9f772b8b4674/html5/thumbnails/30.jpg)
Vida de un Aspecto
• Run Time– Deserialización– RunTimeInitialize (1 vez – la app está lista)– RunTimeInstanceInitialize (cada vez que el target
code se inicializa)– Ejecución del advice
• Ctor() no se ejecuta en Run Time
![Page 31: NET UY Meetup 4 - AOP & PostSharp by Bruno Bologna & Fabian Fernandez](https://reader035.vdocuments.us/reader035/viewer/2022070302/547eef7eb4af9f772b8b4674/html5/thumbnails/31.jpg)
OnMethodBoundaryAspect
• Aspecto a nivel de método– OnEntry– OnExit– OnException– OnSuccess
![Page 32: NET UY Meetup 4 - AOP & PostSharp by Bruno Bologna & Fabian Fernandez](https://reader035.vdocuments.us/reader035/viewer/2022070302/547eef7eb4af9f772b8b4674/html5/thumbnails/32.jpg)
OnExceptionAspect
• Aspecto a nivel de método– OnException– GetExceptionType
![Page 33: NET UY Meetup 4 - AOP & PostSharp by Bruno Bologna & Fabian Fernandez](https://reader035.vdocuments.us/reader035/viewer/2022070302/547eef7eb4af9f772b8b4674/html5/thumbnails/33.jpg)
LocationInterceptionAspect
• Aspecto a nivel de propiedades y campos• Permite interceptar Get y Set– OnGetValue– OnSetValue
• Operaciones útiles– ProceedGetValue()– ProceedSetValue()– GetCurrentValue()– SetNewValue()
![Page 34: NET UY Meetup 4 - AOP & PostSharp by Bruno Bologna & Fabian Fernandez](https://reader035.vdocuments.us/reader035/viewer/2022070302/547eef7eb4af9f772b8b4674/html5/thumbnails/34.jpg)
EventInterceptionAspect
• Aspecto a nivel de eventos– OnAddHandler (+=)– OnRemoveHandler(-=)– OnInvokeHandler
• Operaciones útiles– ProceedAddHandler()– ProceedRemoveHandler()
![Page 35: NET UY Meetup 4 - AOP & PostSharp by Bruno Bologna & Fabian Fernandez](https://reader035.vdocuments.us/reader035/viewer/2022070302/547eef7eb4af9f772b8b4674/html5/thumbnails/35.jpg)
![Page 36: NET UY Meetup 4 - AOP & PostSharp by Bruno Bologna & Fabian Fernandez](https://reader035.vdocuments.us/reader035/viewer/2022070302/547eef7eb4af9f772b8b4674/html5/thumbnails/36.jpg)
Aplicando P# a codigo existente
• Buscar código que sea claramente un patrón decorador
• Buscar aspectos que no interactuen con el target code
• Elegir la mejor forma de agregarlos: multicast o atributos individuales– Individual = más control– Multicast = más fácil pero el código debe estar bien
organizado para tener control• TEST, TEST, TEST!
![Page 37: NET UY Meetup 4 - AOP & PostSharp by Bruno Bologna & Fabian Fernandez](https://reader035.vdocuments.us/reader035/viewer/2022070302/547eef7eb4af9f772b8b4674/html5/thumbnails/37.jpg)
Aplicando P# a codigo existente
• Verificar que no hay efectos secundarios– Considerar los aspectos como islas de alta
encapsulación– Sólo lectura de la meta data del target code– No intentar alterar el flujo de ejecución
![Page 38: NET UY Meetup 4 - AOP & PostSharp by Bruno Bologna & Fabian Fernandez](https://reader035.vdocuments.us/reader035/viewer/2022070302/547eef7eb4af9f772b8b4674/html5/thumbnails/38.jpg)
Aplicando P# a codigo existente
• Arrancar con lo más papa!– Logging & Tracing– Monitoreo de performance– Caching– Exception handling
![Page 39: NET UY Meetup 4 - AOP & PostSharp by Bruno Bologna & Fabian Fernandez](https://reader035.vdocuments.us/reader035/viewer/2022070302/547eef7eb4af9f772b8b4674/html5/thumbnails/39.jpg)
Deployment
• PostSharp.dll y listo!• Si usas ILMarge– Merge de varios assemblies en uno– Se complica un poco!
• Si firmas los assemblies, usar delay signing• Obfuscas tu código?– Sólo soporta dotFuscator
![Page 40: NET UY Meetup 4 - AOP & PostSharp by Bruno Bologna & Fabian Fernandez](https://reader035.vdocuments.us/reader035/viewer/2022070302/547eef7eb4af9f772b8b4674/html5/thumbnails/40.jpg)
![Page 41: NET UY Meetup 4 - AOP & PostSharp by Bruno Bologna & Fabian Fernandez](https://reader035.vdocuments.us/reader035/viewer/2022070302/547eef7eb4af9f772b8b4674/html5/thumbnails/41.jpg)
GRACIAS!
![Page 42: NET UY Meetup 4 - AOP & PostSharp by Bruno Bologna & Fabian Fernandez](https://reader035.vdocuments.us/reader035/viewer/2022070302/547eef7eb4af9f772b8b4674/html5/thumbnails/42.jpg)
Referencias
• Dynamic Decorator Pattern http://goo.gl/DkIe1t • Dynamic Proxies http://goo.gl/ql6Zly • Documentación PostSharp http://goo.gl/mH3l6J • White-paper AOP http://goo.gl/Mtzy0u • Artículo Técnico PostSharp http://goo.gl/NxD380 • Presentaciones AOP http://goo.gl/v6OYmm • Aspect Oriented Programming in .NET http://
goo.gl/HNKlaA • PostSharp Fundamentals http://goo.gl/LPIawa • ILSpy http://ilspy.net/