Loading presentation...

Present Remotely

Send the link below via email or IM

Copy

Present to your audience

Start remote presentation

  • Invited audience members will follow you as you navigate and present
  • People invited to a presentation do not need a Prezi account
  • This link expires 10 minutes after you close the presentation
  • A maximum of 30 users can follow your presentation
  • Learn more about this feature in our knowledge base article

Do you really want to delete this prezi?

Neither you, nor the coeditors you shared it with will be able to recover it again.

DeleteCancel

Make your likes visible on Facebook?

Connect your Facebook account to Prezi and let your likes appear on your timeline.
You can change this under Settings & Account at any time.

No, thanks

Aspect Oriented Programming using POSTSharp

No description
by

Srihari Sridharan

on 2 August 2013

Comments (0)

Please log in to add your comment.

Report abuse

Transcript of Aspect Oriented Programming using POSTSharp

Aspect Oriented Programming using POSTSharp ~ Srihari Sridharan
Aspect Oriented Programming using PostSharp
What is AOP?
Managing code in cross cutting concerns
Increasing the modularity of our code base
Composition of code infrastructure concerns

PostSharp Installation
Out-of-the-Box PostSharp Aspects
Existing Aspects built into PostSharp
Majority use overrides to provide advices to the target code
Decorator pattern is everywhere
Attaching Aspects
Two main techniques
Individual attributes
Multicast attribute
Neither affects how you write your aspect
Both are seamlessly picked up during the post compilation processing
Modifies and/or creates Custom.After.Microsoft.Commons.targets
C:\Program Files\MSBuild\vX.X\
C:\Program Files (x86)\MSBuild\vX.X\
Visual Studio gets:
PostSharp extension
Aspect Browser window
Learn PostSharp window
Nuget
Contains
Run-time library (postsharp.dll)
PostSharp Extension

Adds a reference to the PostSharp.targets to include P# in the build process

Removes references to past versions of P#

Helps to uninstall past versions of the Extension
Licensing
Licensing
Manual
License Server
OnMethodBoundaryAspect
Method level aspect
OnEntry
OnExit
OnException
OnSuccess
Maps to a full decorator pattern implementation when all four overrides are used
OnMethodBoundaryAspect Structure
OnExceptionAspect
Method level aspect
OnException
GetExceptionType

Wraps the entire method in a try…catch block

Used to trap exceptions that are thrown by target code
OnExceptionAspect
Structure
LocationInterceptionAspect
Property and Field level aspect
Allows interception of Get and Set actions
OnGetValue
OnSetValue
Executing target code is done using:
ProceedGetValue()
ProceedSetValue()
GetCurrentValue()
SetNewValue()
LocationInterceptionAspect Structure
EventInterceptionAspect
Event level aspect
Allows interception of event handler actions
OnAddHandler (+=)
OnRemoveHandler (-=)
OnInvokeHandler
Replaces the target event that was to be raised
Execute target code with ProceedInvokeHandler
Execute target code using
ProceedAddHandler
ProceedRemoveHandler
Individual Attributes
All OOTB aspects are also attributes
Attributes need to decorate all of the locations where the aspect should be applied
Can be seen as code duplication (not DRY)
Can be seen as tighter coupling of aspects to target code
Multicast Attribute
A custom attribute implementation included with PostSharp
Declaration of the attribute is done in AssemblyInfo
Has same capabilities as individual attributes
More of a convention based approach
Target code is no longer aware of attached aspects
Why AOP?
Reduce code duplication
Keep code singly responsible
Modularity
How AOP?
Interceptors
Basic Decorator pattern
Commonly implemented through IoC containers
IL Weaving
Much more complex possibilities
Requires a post compile step
Aspect Lifetime
Compile Time Steps
Aspect Instantiation
CompileTimeValidation
CompileTimeInitialization
Serialization
Executed during the post compilation processing
CompileTimeInitialize
CompileTimeValidate
Shouldn’t depend on each other
May reference same field level data
Aspect Lifetime cont.
Run Time Steps
Deserialization
RuntimeInitialize
RuntimeInstanceInitialize
Advice execution
Aspect constructor is not executed at run time
RunTimeInitialize
Replacement for when the aspect’s constructor would be run
RunTimeInstanceInitialize
Only run for IInstanceScopedAspect implementors
Executed when the target’s instance is initialized
Should only be used for tasks that require the main application to be in an operational state
Exceptions
Silverlight, Windows Phone, Compact F/W
Compile time actions
Aspect Instantiation - n/a
CompileTimeValidation - n/a
CompileTimeInitialization - n/a
Serialization
Run time actions
Deserialization
RuntimeInitialize
RuntimeInstanceInitialize
Advice execution
Silverlight, Compact F/W
Only ever use binary serialization
Partial Trust
Binary serialization will not work
In short...
Compile time code execution is possible
Aspects instances are serialized to managed resources at compile time
Run time initialization of the aspects
Some target platforms don’t support compile time actions
Using PostSharp in
Brownfield Applications
Approach
Looks for pure decorator pattern implementations
Implement aspects that don’t require interaction with target code
Attach aspects
Watch for side effects in target code
Rigorous re-testing
Use individual attribute decoration
Multicasting will be difficult unless the code is organized nicely
Attaching Aspects
Side Effects
Consider aspect to be islands of high encapsulation
Only read current state and meta data about target code
Don’t try to short circuit target code execution
Watch for unintended side effects
Lazy loading being triggered
Objects being marked ‘dirty’
Low Hanging Fruits
Logging & Tracing
Performance Monitoring
Caching
In short...
PostSharp is the easiest way to do AOP in Brownfield situations
Aspects can have unintended side effects
Keep aspects simple to avoid major refactoring
PostSharp.dll
ILMerge
Aspects are serialized and have aspect naming info
During deserialization
Must notify the serializer that the aspect will be in a different location
Needs to be added before the first aspect is hit
Program.Main
Global.asax Application_Start
App.xaml handling the Application.Startup event

Deployment
Signing Assemblies
Use delay signing
Workflow becomes
Compile with public key
Post compile processing by PostSharp
Sign with the Strong Name tool
Obfuscating Assemblies
Not available prior to v2.1
Obfuscator must be supported (Dotfuscator)
Process
Assembly compiled
Assembly is post processed by PostSharp
Assembly is obfuscated
Post obfuscation table name repair by PostSharp
Developer Experience in
Visual Studio IDE
Stepping into targets
Breakpoints in aspects
Watch windows
Stack trace window
Compile time execution alone cannot be debugged - no process to attach
Pipe Server - Improves build performance
Started during project needs to be built
Stops after prolonged period of inactivity
Full transcript