Top Banner
Aspect-oriented programming robert maclean www.sadev.co.za ∞ @rmaclean ∞ [email protected]
16

Aspect oriented programming

Nov 17, 2014

Download

Technology

Robert MacLean

AOP is a way of making code easier to write and work with by extracting cross cutting concerns into small easily reusable code that is applied with attribututes. This session looks at that and the various options available to us, from free items to commercial offerings like PostSharp.
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Aspect oriented programming

Aspect-oriented programming

robert macleanwww.sadev.co.za ∞ @rmaclean ∞

[email protected]

Page 2: Aspect oriented programming

Agenda

• What, when, why of AOP?• How is it implemented in PostSharp

and how does it work?• Alternatives to PostSharp• Looking to the future• Discussion

Page 3: Aspect oriented programming

Every project has “boring” code

source: Microsoft Application Architecture Guide, 2nd Edition

Page 4: Aspect oriented programming

AOP Details

• Goal to make cross cutting concerns easy to implement and focus on business code

• Xerox PARC in 2001• AspectJ• Many implementations now days• AKA: AOSD

Page 5: Aspect oriented programming

Scenario code – simple prime calculator

Customer says it’s wrong. We need logging to diagnose…

Page 6: Aspect oriented programming

Let’s add logging – trace input & output

5 extra lines of code

Easy to miss some places

Lots of repetition of code

Need to teach this

Not portable

Page 7: Aspect oriented programming

What about some procedural help?

15 extra lines of code

Not junior friendly

Need 1 per signature

Page 8: Aspect oriented programming

So how does aspects solve that?

1 extra line of code

Feel like you are missing something?

Page 9: Aspect oriented programming

Demo time…

Page 10: Aspect oriented programming

Under the hood

Source: .NET Reflector 6.8

IL modified

Page 11: Aspect oriented programming

Many ways to implement AOP in .NET currently

• Popular: – IL Weaving @ compile or JIT– PostSharp & Mono.Cecil

• Less:– Hooking into .NET profiler API– Proxies

Page 12: Aspect oriented programming

Hooking the profiler

CLR

Application

Profiler

Environmental

COM API

Disadvantages• Poor performance• Exceptionally

complex• Not designed for this

Advantages• Nothing is safe from

it’s power• Solves some fringe

cases (i.e. mocking static methods)

Page 13: Aspect oriented programming

Proxies

Object

Method Call

Proxy

Method Call

Object

Normal Proxied

Disadvantages• Complex with ContextBoundObject• Perf is worse than IL weavingAdvantages• Using DI it can be easier• Perf is better than .NET profiler

Page 14: Aspect oriented programming

Future: Post Roslyn Theory

• Roslyn– Next version of C#/VB “compiler”– Written in managed code– Available as a service

• Post Roslyn– Insert new language semantics – Approach the AspectJ style

Page 15: Aspect oriented programming

Summary

• AOP–Way to untangle cross cutting concerns

out of business code– Great re-use– Can be easy to use (depending on

implementation)–Many options to do this!

Page 16: Aspect oriented programming

Questions & Discussions