introduction to aspect oriented programming (ddd south west 4.0)

59
Introductio n to Yan Cui Aspect Oriented Programming by theburningmonk.com @theburningmonk

Upload: yan-cui

Post on 22-Jun-2015

5.338 views

Category:

Technology


0 download

DESCRIPTION

Introduction to AOP talk at DDD SouthWest 4.0, including examples of AOP using dynamic proxies, functional programming, dynamic language and PostSharp.

TRANSCRIPT

Page 1: Introduction to Aspect Oriented Programming (DDD South West 4.0)

Introduction to

Yan Cui

Aspect Oriented Programming

bytheburningmonk.com@theburningmonk

Page 2: Introduction to Aspect Oriented Programming (DDD South West 4.0)

Server-side Developer @

Page 3: Introduction to Aspect Oriented Programming (DDD South West 4.0)

600K+ DAU150M reqs/day

2500 reqs/s

15K IO op/s 200+ servers

75ms avg latency

40K+ concurrent users

Page 4: Introduction to Aspect Oriented Programming (DDD South West 4.0)

• Overview Cross-Cutting Concerns AOP What’s in it for you

• AOP Terminologies AOP and OOP Solutions

• Q&A

Page 5: Introduction to Aspect Oriented Programming (DDD South West 4.0)

CROSS-CUTTING CONCERNS

the PROBLEM...

Page 6: Introduction to Aspect Oriented Programming (DDD South West 4.0)

Image by Mike Rohde

Cross-Cutting

Concerns

Page 7: Introduction to Aspect Oriented Programming (DDD South West 4.0)

• Cuts across multiple abstractions

• Difficult to decompose

• High-coupling

• Boilerplate code

Page 8: Introduction to Aspect Oriented Programming (DDD South West 4.0)

• Code tangling and scattering Poor traceability

Lower productivity

Less code reuse

Harder refactoring

Page 9: Introduction to Aspect Oriented Programming (DDD South West 4.0)

ASPECT-ORIENTED PROGRAMMING

the SOLUTION...

Page 10: Introduction to Aspect Oriented Programming (DDD South West 4.0)

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

- wikipedia

Page 11: Introduction to Aspect Oriented Programming (DDD South West 4.0)

AO

P

AOP

AO

PA

OP

AO

P

AO

P

AO

P

Page 12: Introduction to Aspect Oriented Programming (DDD South West 4.0)

Image by Mike Rohde

AOP

Page 13: Introduction to Aspect Oriented Programming (DDD South West 4.0)
Page 14: Introduction to Aspect Oriented Programming (DDD South West 4.0)

• Centralize concerns implementation

• Intercept method calls

• Inject new behaviour

• More reusable code

• Cleaner code

Page 15: Introduction to Aspect Oriented Programming (DDD South West 4.0)

What’s in it for YOU?

Page 16: Introduction to Aspect Oriented Programming (DDD South West 4.0)

• Write less code

• Read less code

• More concise and easy to understand

• More maintainable

Page 17: Introduction to Aspect Oriented Programming (DDD South West 4.0)

• Fewer code =

• Less boilerplate code

• More interesting work

• Increased attention

• More PRODUCTIVITY!

FEWER DEFECTS!

Page 18: Introduction to Aspect Oriented Programming (DDD South West 4.0)

AOPdive a little deeper into...

Page 19: Introduction to Aspect Oriented Programming (DDD South West 4.0)

• Join Point Place where behaviour can be added

start/end of method

property getter/setter

...

Page 20: Introduction to Aspect Oriented Programming (DDD South West 4.0)

• Advice Code that’s injected at join points

Logging

Validation

...

Page 21: Introduction to Aspect Oriented Programming (DDD South West 4.0)

• Point cut Join points where advice should be

applied

Page 22: Introduction to Aspect Oriented Programming (DDD South West 4.0)

• Aspect Container holding point cuts and advice

Aspect is to AOP what class is to OOP

Page 23: Introduction to Aspect Oriented Programming (DDD South West 4.0)

• Weaving Combines advices with point cuts

Page 24: Introduction to Aspect Oriented Programming (DDD South West 4.0)

• AOP is complementary to OOP

• AOP targets a specific problem

• Code modularization OOP – Real world objects

AOP – Functionalities

Page 25: Introduction to Aspect Oriented Programming (DDD South West 4.0)

• Help you S.O.L.I.Dify your code Single responsibility

Open/close

Page 26: Introduction to Aspect Oriented Programming (DDD South West 4.0)

• You can do AOP via: Dynamic Proxies

Functional Programming

Code Generation

Dynamic Languages

Static Weaving

Page 27: Introduction to Aspect Oriented Programming (DDD South West 4.0)

DYNAMIC PROXIES

AOP via...

Page 28: Introduction to Aspect Oriented Programming (DDD South West 4.0)

• IoC framesworks Castle

Spring.Net

• Dynamic interceptors

Page 29: Introduction to Aspect Oriented Programming (DDD South West 4.0)

Image by Mike Rohde

Dynamic Proxies

Page 30: Introduction to Aspect Oriented Programming (DDD South West 4.0)
Page 31: Introduction to Aspect Oriented Programming (DDD South West 4.0)

• Advantages Can use existing DI framework

Built-in aspects (logging, etc.)

Aspects can be configured after build

Page 32: Introduction to Aspect Oriented Programming (DDD South West 4.0)

• 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

Page 33: Introduction to Aspect Oriented Programming (DDD South West 4.0)

Class, is AOP the same as Dependency Injection?

NOOOO Sir!!

Page 34: Introduction to Aspect Oriented Programming (DDD South West 4.0)

FUNCTIONAL PROGRAMMING

AOP via...

Page 35: Introduction to Aspect Oriented Programming (DDD South West 4.0)

Image by Mike Rohde

Functional

Programming

Page 36: Introduction to Aspect Oriented Programming (DDD South West 4.0)
Page 37: Introduction to Aspect Oriented Programming (DDD South West 4.0)

• Advantages No external dependencies

Page 38: Introduction to Aspect Oriented Programming (DDD South West 4.0)

• Disadvantages Requires modification to every function

No support for matching rules

Manual aspect composition

Page 39: Introduction to Aspect Oriented Programming (DDD South West 4.0)

CODE GENERATION

AOP via...

Page 40: Introduction to Aspect Oriented Programming (DDD South West 4.0)

• T4, CodeSmith tools

Page 41: Introduction to Aspect Oriented Programming (DDD South West 4.0)

• Advantages Easy to generate complex source code

Page 42: Introduction to Aspect Oriented Programming (DDD South West 4.0)

• Disadvantages Input is usually XML

Cannot inject new behaviour to existing

code

Page 43: Introduction to Aspect Oriented Programming (DDD South West 4.0)

DYNAMIC LANGUAGES

AOP via...

Page 44: Introduction to Aspect Oriented Programming (DDD South West 4.0)

Image by Mike Rohde

Dynamic

Languages

Page 45: Introduction to Aspect Oriented Programming (DDD South West 4.0)

• Advantages Meta-programming is easy

Page 46: Introduction to Aspect Oriented Programming (DDD South West 4.0)

• Disadvantages Switch to a dynamic language can be hard

No (limited) Visual Studio tooling

Page 47: Introduction to Aspect Oriented Programming (DDD South West 4.0)

STATIC WEAVING

AOP via...

Page 48: Introduction to Aspect Oriented Programming (DDD South West 4.0)

• PostSharp, AspectJ

• Uses low-level MSIL transformation

• Compile time

Page 49: Introduction to Aspect Oriented Programming (DDD South West 4.0)

Aspect Decomposition

Aspects Core program

Aspect Recomposition

Requirements

Final System

Page 50: Introduction to Aspect Oriented Programming (DDD South West 4.0)

Image by Mike Rohde

PostSharp

Page 51: Introduction to Aspect Oriented Programming (DDD South West 4.0)

• Advantages Most complete support of AOP features

Aspect usage is verified at build time

Better runtime performance

Page 52: Introduction to Aspect Oriented Programming (DDD South West 4.0)

• Disadvantages Increased build time

New to many developers

Page 53: Introduction to Aspect Oriented Programming (DDD South West 4.0)

PostSharp Examples• Auto-implement INotifyPropertyChanged

• Undo/Redo

• Thread dispatching

• Transaction handling

• Performance Monitoring

Page 54: Introduction to Aspect Oriented Programming (DDD South West 4.0)

• Performance Monitoring Method execution time

Method execution count

Error count

Page 55: Introduction to Aspect Oriented Programming (DDD South West 4.0)

Record exec time

Publish to Amazon CloudWatch

Page 56: Introduction to Aspect Oriented Programming (DDD South West 4.0)
Page 57: Introduction to Aspect Oriented Programming (DDD South West 4.0)

• Amazon CloudWatch Cheap to run

Supports alarms and notifications

Visualization tool

Page 58: Introduction to Aspect Oriented Programming (DDD South West 4.0)

• http://bit.ly/KRDdgr

Page 59: Introduction to Aspect Oriented Programming (DDD South West 4.0)

Thank You!

@theburningmonktheburningmonk.com