introduction to aspect oriented programming (ddd south west 4.0)
DESCRIPTION
Introduction to AOP talk at DDD SouthWest 4.0, including examples of AOP using dynamic proxies, functional programming, dynamic language and PostSharp.TRANSCRIPT
Introduction to
Yan Cui
Aspect Oriented Programming
bytheburningmonk.com@theburningmonk
Server-side Developer @
600K+ DAU150M reqs/day
2500 reqs/s
15K IO op/s 200+ servers
75ms avg latency
40K+ concurrent users
• Overview Cross-Cutting Concerns AOP What’s in it for you
• AOP Terminologies AOP and OOP Solutions
• Q&A
CROSS-CUTTING CONCERNS
the PROBLEM...
Image by Mike Rohde
Cross-Cutting
Concerns
• Cuts across multiple abstractions
• Difficult to decompose
• High-coupling
• Boilerplate code
• Code tangling and scattering Poor traceability
Lower productivity
Less code reuse
Harder refactoring
ASPECT-ORIENTED PROGRAMMING
the SOLUTION...
“AOP is a programming paradigm which aims to increase modularity by allowing the separation of cross-cutting concerns”
- wikipedia
AO
P
AOP
AO
PA
OP
AO
P
AO
P
AO
P
Image by Mike Rohde
AOP
• Centralize concerns implementation
• Intercept method calls
• Inject new behaviour
• More reusable code
• Cleaner code
What’s in it for YOU?
• Write less code
• Read less code
• More concise and easy to understand
• More maintainable
• Fewer code =
• Less boilerplate code
• More interesting work
• Increased attention
• More PRODUCTIVITY!
FEWER DEFECTS!
AOPdive a little deeper into...
• Join Point Place where behaviour can be added
start/end of method
property getter/setter
...
• Advice Code that’s injected at join points
Logging
Validation
...
• Point cut Join points where advice should be
applied
• Aspect Container holding point cuts and advice
Aspect is to AOP what class is to OOP
• Weaving Combines advices with point cuts
• AOP is complementary to OOP
• AOP targets a specific problem
• Code modularization OOP – Real world objects
AOP – Functionalities
• Help you S.O.L.I.Dify your code Single responsibility
Open/close
• You can do AOP via: Dynamic Proxies
Functional Programming
Code Generation
Dynamic Languages
Static Weaving
DYNAMIC PROXIES
AOP via...
• IoC framesworks Castle
Spring.Net
• Dynamic interceptors
Image by Mike Rohde
Dynamic Proxies
• Advantages Can use existing DI framework
Built-in aspects (logging, etc.)
Aspects can be configured after build
• 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
Class, is AOP the same as Dependency Injection?
NOOOO Sir!!
FUNCTIONAL PROGRAMMING
AOP via...
Image by Mike Rohde
Functional
Programming
• Advantages No external dependencies
• Disadvantages Requires modification to every function
No support for matching rules
Manual aspect composition
CODE GENERATION
AOP via...
• T4, CodeSmith tools
• Advantages Easy to generate complex source code
• Disadvantages Input is usually XML
Cannot inject new behaviour to existing
code
DYNAMIC LANGUAGES
AOP via...
Image by Mike Rohde
Dynamic
Languages
• Advantages Meta-programming is easy
• Disadvantages Switch to a dynamic language can be hard
No (limited) Visual Studio tooling
STATIC WEAVING
AOP via...
• PostSharp, AspectJ
• Uses low-level MSIL transformation
• Compile time
Aspect Decomposition
Aspects Core program
Aspect Recomposition
Requirements
Final System
Image by Mike Rohde
PostSharp
• Advantages Most complete support of AOP features
Aspect usage is verified at build time
Better runtime performance
• Disadvantages Increased build time
New to many developers
PostSharp Examples• Auto-implement INotifyPropertyChanged
• Undo/Redo
• Thread dispatching
• Transaction handling
• Performance Monitoring
• Performance Monitoring Method execution time
Method execution count
Error count
Record exec time
Publish to Amazon CloudWatch
• Amazon CloudWatch Cheap to run
Supports alarms and notifications
Visualization tool
• http://bit.ly/KRDdgr
Thank You!
@theburningmonktheburningmonk.com