introduction to aop. programming paradigm which aims to increase modularity by allowing the...

26
Introduction to AOP

Upload: roland-hunt

Post on 27-Dec-2015

214 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Introduction to AOP. Programming paradigm which aims to increase modularity by allowing the separation of cross-cutting concerns en.wikipedia.org/wiki/aspect-oriented-programming

Introductionto AOP

Page 2: Introduction to AOP. Programming paradigm which aims to increase modularity by allowing the separation of cross-cutting concerns en.wikipedia.org/wiki/aspect-oriented-programming

Programming paradigm which aims to increase

modularity by allowing the separation of

cross-cutting concernsen.wikipedia.org/wiki/aspect-oriented-programming

Programming paradigm which aims to increase

modularity by allowing the separation of

cross-cutting concernsen.wikipedia.org/wiki/aspect-oriented-programming

Page 3: Introduction to AOP. Programming paradigm which aims to increase modularity by allowing the separation of cross-cutting concerns en.wikipedia.org/wiki/aspect-oriented-programming

Programming Paradigm

It is *NOT*- a programming language- solution to all your

problems- a first class citizen in Java

or .NET- a replacement for OOP

It is- a programming pattern

- a tool in your toolbox

- an easily added language feature

- complimentary to OOP

Page 4: Introduction to AOP. Programming paradigm which aims to increase modularity by allowing the separation of cross-cutting concerns en.wikipedia.org/wiki/aspect-oriented-programming

Modularity

Aspects can…• Exist in isolated and encapsulated constructs• Be attached to almost any standard code construct• Be transported between projects

Page 5: Introduction to AOP. Programming paradigm which aims to increase modularity by allowing the separation of cross-cutting concerns en.wikipedia.org/wiki/aspect-oriented-programming

Cross-Cutting Concerns

…aspects of a program that affect other concerns. These concerns often cannot be cleanly

decomposed from the rest of the system in both the design and implementation,

and can result in either scattering (code duplication),

tangling (significant dependencies between systems), or both.

http://en.wikipedia.org/wiki/Cross-cutting_concern

Page 6: Introduction to AOP. Programming paradigm which aims to increase modularity by allowing the separation of cross-cutting concerns en.wikipedia.org/wiki/aspect-oriented-programming

…more simply

Any code functionality that repeats itself in a regular pattern across many unrelated tiers/layers

within a codebase.

Page 7: Introduction to AOP. Programming paradigm which aims to increase modularity by allowing the separation of cross-cutting concerns en.wikipedia.org/wiki/aspect-oriented-programming
Page 8: Introduction to AOP. Programming paradigm which aims to increase modularity by allowing the separation of cross-cutting concerns en.wikipedia.org/wiki/aspect-oriented-programming

Cross-Cutting Concerns

Services

Domain Model

Repositories

Model

ViewModel

View

Logg

ing Se

curit

y

Audi

ting

Valid

ation

Undo/Redo

Thread Management

INotifyPropertyChanged

Unit of Work Management

Circuit Breaker

Page 9: Introduction to AOP. Programming paradigm which aims to increase modularity by allowing the separation of cross-cutting concerns en.wikipedia.org/wiki/aspect-oriented-programming

AOP Styles

1. InterceptionInterjecting the execution of cross-cutting concerns at run time.Non-invasive approach to modifying the code execution path.

2. IL WeavingInterjecting the planned execution of cross-cutting concerns during the compilation process.Invasive approach to modifying the code execution path.

Page 10: Introduction to AOP. Programming paradigm which aims to increase modularity by allowing the separation of cross-cutting concerns en.wikipedia.org/wiki/aspect-oriented-programming

Interception

• Existing in IoC container implementations• Follows a very strict decorator pattern• Likely implemented using weak typing• Run-time operation• Can be applied en-masse• Easy to implement if you’re already using IoC

Page 11: Introduction to AOP. Programming paradigm which aims to increase modularity by allowing the separation of cross-cutting concerns en.wikipedia.org/wiki/aspect-oriented-programming

Decorator Pattern

//new functionality before//the code that already exists//new functionality after

//exception handling

Page 12: Introduction to AOP. Programming paradigm which aims to increase modularity by allowing the separation of cross-cutting concerns en.wikipedia.org/wiki/aspect-oriented-programming

Weak Typing

Page 13: Introduction to AOP. Programming paradigm which aims to increase modularity by allowing the separation of cross-cutting concerns en.wikipedia.org/wiki/aspect-oriented-programming

En-masse Attachment

Page 14: Introduction to AOP. Programming paradigm which aims to increase modularity by allowing the separation of cross-cutting concerns en.wikipedia.org/wiki/aspect-oriented-programming

IL Weaving

• Post compilation process• Alters the assembly/executable at an IL level• Scary to people• Can be attached to almost every code construct• Very rare that it required changes to existing code• Run and compile time operations• Can be much easier to add to legacy code

Page 15: Introduction to AOP. Programming paradigm which aims to increase modularity by allowing the separation of cross-cutting concerns en.wikipedia.org/wiki/aspect-oriented-programming

The Process

Write CodeCompile

Application exe/dll

AOP post compiler/weav

er

Throw compile-time

error

exe/dllDeploy

Page 16: Introduction to AOP. Programming paradigm which aims to increase modularity by allowing the separation of cross-cutting concerns en.wikipedia.org/wiki/aspect-oriented-programming

Constructs

• Methods (private/public/internal/etc)• Properties• Field level variables• Events• Event registration/de-registration• Code generation

Page 17: Introduction to AOP. Programming paradigm which aims to increase modularity by allowing the separation of cross-cutting concerns en.wikipedia.org/wiki/aspect-oriented-programming

Run Time vs Compile Time

Run Time• All aspect code executes in application context

Compile Time• Code can be designated to run during post-compilation• Compilation can fail for correct syntaxes but incorrect

business logic

Page 18: Introduction to AOP. Programming paradigm which aims to increase modularity by allowing the separation of cross-cutting concerns en.wikipedia.org/wiki/aspect-oriented-programming

Tooling

• Interception• Castle Winsdor• StructureMap• Ninject (with Ninject.Extensions)• AutoFac (with Autofac.Extras)

• IL Weaving• PostSharp (current)• LinFu

Page 19: Introduction to AOP. Programming paradigm which aims to increase modularity by allowing the separation of cross-cutting concerns en.wikipedia.org/wiki/aspect-oriented-programming

The Traditional Arguments Against AOP

• It is “magic”• We won’t be able to debug properly• How do we know where aspects will be executed?• It’s another thing we have to learn• Changing the aspect will break the entire application

Page 20: Introduction to AOP. Programming paradigm which aims to increase modularity by allowing the separation of cross-cutting concerns en.wikipedia.org/wiki/aspect-oriented-programming

“Its magic!” & “We can’t debug it!”

• Both interception and IL weaving provide full line-by-line debugging• Attachment of aspects is explicit as you want it to be

Page 21: Introduction to AOP. Programming paradigm which aims to increase modularity by allowing the separation of cross-cutting concerns en.wikipedia.org/wiki/aspect-oriented-programming

“But which aspect(s) will run?”

• This is a tooling/discoverability problem, not a problem with AOP itself• PostSharp has very good VS integration showing what

aspects are attached to a piece of code• Interceptor discoverability is a bigger problem

Page 22: Introduction to AOP. Programming paradigm which aims to increase modularity by allowing the separation of cross-cutting concerns en.wikipedia.org/wiki/aspect-oriented-programming

“Its another thing we have to learn”

Is it worse if we train them and they leave or if we don’t

train them and they stay?

Page 23: Introduction to AOP. Programming paradigm which aims to increase modularity by allowing the separation of cross-cutting concerns en.wikipedia.org/wiki/aspect-oriented-programming

“Changing the aspect can break the entire application!”

• You can probably do that in an number of different ways with any codebase that doesn’t use AOP• This is a problem caused by tight coupling and poor

separation of concerns

Page 24: Introduction to AOP. Programming paradigm which aims to increase modularity by allowing the separation of cross-cutting concerns en.wikipedia.org/wiki/aspect-oriented-programming

Your Project is Already Established…

You use IoC• For logging, caching and possibly transaction

management use interception• If the cross cutting concerns are more complex

(Undo/Redo, thread management) use IL Weaving

You don’t use IoC• The only real option is to use IL Weaving

Page 25: Introduction to AOP. Programming paradigm which aims to increase modularity by allowing the separation of cross-cutting concerns en.wikipedia.org/wiki/aspect-oriented-programming

You’re Starting a New Project…

You have AOP buy-in from senior developers• Start with IL Weaving, but start with simple, pre-built

aspects• Write custom aspects as experience and need grows

AOP is still unknown to the senior people on the team• Start with IL Weaving, but start in a very isolated

manner• Use this as a proof of concept until you have senior

level buy-in

Page 26: Introduction to AOP. Programming paradigm which aims to increase modularity by allowing the separation of cross-cutting concerns en.wikipedia.org/wiki/aspect-oriented-programming

gracias

Donald Belcham@dbelcham

[email protected]