Slide 1

Slide 1 text

Introduction to Yan Cui Aspect Oriented Programming by theburningmonk.com @theburningmonk

Slide 2

Slide 2 text

Server-side Developer @

Slide 3

Slide 3 text

No content

Slide 4

Slide 4 text

• Overview  Cross-Cutting Concerns  AOP  What’s in it for you • AOP  Terminologies  AOP and OOP  Solutions • Q&A

Slide 5

Slide 5 text

CROSS-CUTTING CONCERNS the PROBLEM...

Slide 6

Slide 6 text

Image by Mike Rohde Cross-Cutting Concerns

Slide 7

Slide 7 text

• Cuts across multiple abstractions • Difficult to decompose • High-coupling • Boilerplate code

Slide 8

Slide 8 text

• Code tangling and scattering  Poor traceability  Lower productivity  Less code reuse  Harder refactoring

Slide 9

Slide 9 text

ASPECT-ORIENTED PROGRAMMING the SOLUTION...

Slide 10

Slide 10 text

“AOP is a programming paradigm which aims to increase modularity by allowing the separation of cross-cutting concerns” - wikipedia

Slide 11

Slide 11 text

No content

Slide 12

Slide 12 text

Image by Mike Rohde AOP

Slide 13

Slide 13 text

No content

Slide 14

Slide 14 text

• Centralize concerns implementation • Intercept method calls • Inject new behaviour • More reusable code • Cleaner code

Slide 15

Slide 15 text

What’s in it for YOU?

Slide 16

Slide 16 text

• Write less code • Read less code • More concise and easy to understand • More maintainable

Slide 17

Slide 17 text

• Fewer code = • Less boilerplate code • More interesting work • Increased attention • More PRODUCTIVITY! FEWER DEFECTS!

Slide 18

Slide 18 text

AOP dive a little deeper into...

Slide 19

Slide 19 text

• Join Point  Place where behaviour can be added  start/end of method  property getter/setter  ...

Slide 20

Slide 20 text

• Advice  Code that’s injected at join points  Logging  Validation  ...

Slide 21

Slide 21 text

• Point cut  Join points where advice should be applied

Slide 22

Slide 22 text

• Aspect  Container holding point cuts and advice  Aspect is to AOP what class is to OOP

Slide 23

Slide 23 text

• Weaving  Combines advices with point cuts

Slide 24

Slide 24 text

• AOP is complementary to OOP • AOP targets a specific problem • Code modularization  OOP – Real world objects  AOP – Functionalities

Slide 25

Slide 25 text

• Help you S.O.L.I.Dify your code  Single responsibility  Open/close

Slide 26

Slide 26 text

• You can do AOP via:  Dynamic Proxies  Functional Programming  Code Generation  Dynamic Languages  Static Weaving

Slide 27

Slide 27 text

DYNAMIC PROXIES AOP via...

Slide 28

Slide 28 text

• IoC framesworks  Castle  Spring.Net • Dynamic interceptors

Slide 29

Slide 29 text

Image by Mike Rohde Dynamic Proxies

Slide 30

Slide 30 text

No content

Slide 31

Slide 31 text

• Advantages  Can use existing DI framework  Built-in aspects (logging, etc.)  Aspects can be configured after build

Slide 32

Slide 32 text

• Disadvantages  Significant change to base code required to adapt  Limited AOP features  Do not work on static, non-public methods  Do not work on fields, properties, or events  Higher run-time overhead  No build-time verification  Objects must be instantiated using the container

Slide 33

Slide 33 text

Class, is AOP the same as Dependency Injection? NOOOO Sir!!

Slide 34

Slide 34 text

FUNCTIONAL PROGRAMMING AOP via...

Slide 35

Slide 35 text

Image by Mike Rohde Functional Programming

Slide 36

Slide 36 text

No content

Slide 37

Slide 37 text

• Advantages  No external dependencies

Slide 38

Slide 38 text

• Disadvantages  Requires modification to every function  No support for matching rules  Manual aspect composition

Slide 39

Slide 39 text

CODE GENERATION AOP via...

Slide 40

Slide 40 text

• T4, CodeSmith tools

Slide 41

Slide 41 text

• Advantages  Easy to generate complex source code

Slide 42

Slide 42 text

• Disadvantages  Input is usually XML  Cannot inject new behaviour to existing code

Slide 43

Slide 43 text

DYNAMIC LANGUAGES AOP via...

Slide 44

Slide 44 text

Image by Mike Rohde Dynamic Languages

Slide 45

Slide 45 text

• Advantages  Meta-programming is easy

Slide 46

Slide 46 text

• Disadvantages  Switch to a dynamic language can be hard  No (limited) Visual Studio tooling

Slide 47

Slide 47 text

STATIC WEAVING AOP via...

Slide 48

Slide 48 text

• PostSharp, AspectJ • Uses low-level MSIL transformation • Compile time

Slide 49

Slide 49 text

Aspect Decomposition Aspects Core program Aspect Recomposition Requirements Final System

Slide 50

Slide 50 text

Image by Mike Rohde PostSharp

Slide 51

Slide 51 text

• Advantages  Most complete support of AOP features  Aspect usage is verified at build time  Better runtime performance

Slide 52

Slide 52 text

• Disadvantages  Increased build time  New to many developers

Slide 53

Slide 53 text

PostSharp Examples • Auto-implement INotifyPropertyChanged • Undo/Redo • Thread dispatching • Transaction handling • Performance Monitoring

Slide 54

Slide 54 text

• Performance Monitoring  Method execution time  Method execution count  Error count

Slide 55

Slide 55 text

Record exec time Publish to Amazon CloudWatch

Slide 56

Slide 56 text

No content

Slide 57

Slide 57 text

• Amazon CloudWatch  Cheap to run  Supports alarms and notifications  Visualization tool

Slide 58

Slide 58 text

• http://bit.ly/KRDdgr

Slide 59

Slide 59 text

Thank You! @theburningmonk theburningmonk.com