AOP with PostSharp

download report

Transcript AOP with PostSharp

NCSoft China PD Dept. training - Session 4
-- Lance Zhang
 What is AOP
 Existing AOP Frameworks in .NET
 Introduce PostSharp
 How does PostSharp work
 How to use PostSharp
Suppose We have a System…
 It Grows…
 Code Smells similar…
 Now We've Got A Problem(Ctrl + C/V)…
 So We Concerns…
Application Concerns
 Functional Requirements
 Line-of-business.
 Non-Functional Concerns
 Logging
 Caching
 Transaction
 Security
 Validation
 Persistence
Aspects - Reduce noise in source
Move plumbing out of source, but keep behaviour
the same
 AOP …
 doesn’t solve any new problems.
 is just too complex.
 adoption is all or nothing.
 Promotes sloppy design.
 Aspects …
 obscure program flow.
 can’t be unit tested.
 Debugging with Aspects is hard.
Aspects Lingo
 The duplicated plumbing code we are removing
 Typically under 30 lines of code
 Behaviour that is injected at a join point
Join points
 Places within the code where the Aspect is inserted
 Examples
 Entry/Exit of a method or property
 Class’s Type Definition
Aspects Lingo
Point cut
 Locates Join Points to apply advice
 Filter driven – Automatic injection
 Find all Methods, of type Setter, in all classes where
Namespace equals “Application.Entities”
 Attribute driven – Manual injection
Aspects Lingo
 Process of injecting functionality back into a
 Can be performed by
 Text post processor – Magic comments can replaced by
 Proxy container – Uses decorator pattern/hooks to allow
code to be inserted
 Binary manipulation - Modifying assemblies - by
replacing and injecting IL code
Existing AOP Frameworks in .NET
 AOP with Castle Windsor Dynamic Proxy
 Dynamic Proxy does the heavy-lifting of the IL
generation for us
 Much easier for most requirements
 AOP with Spring.NET
 Can use something other than attributes such as XML
Sorry, but actually I am
unfamiliar with them…
AOP with PostSharp
 Compile-time MSIL Injection - by MSBuild Task
 Easy to use - like normal Attribute
 Intercept any method - not only Virtual
 More Join points - on: field access, exception
 More controllability - break, change param or return
 More transparent - no need “new proxy()”
 Multicasting Custom Attributes - filter
Code quality after injection
Complexity of Debug
That's Great, But I Need Some Code.
 Originally
we writeby
Then we refector
Okay, Where Is The Plumbing Code?
 Attribute
How Do We Go From Attributes To Aspects?
We can look at an assembly compiled with PostSharp attributes enabled
and disabled to see what happened.
Assembly Before/After Injection
 Before
Atfer Injection
How Can I Implement PostSharp?
 PostSharp.Laos.dll
 Handles most every type of AOP use, it’s the root
namespace for all the demo code we’ll look at.
 Can use commercially, custom non-viral licensing.
 Implemented as a plugin built on the PostSharp.Core
 PostSharp.Public.dll
 Support library for PostSharp.
 PostSharp.AspNet.dll
 Only if you are in an ASP.NET project.
When Can PostSharp Insert Code?
 PostSharp.Laos.OnMethodBoundaryAspect
 OnEntry
 OnExit
 OnSuccess
 OnException
 PostSharp.Laos.OnMethodInvocationAspect
 OnInvocation
 PostSharp.Laos.OnExceptionAspect
 OnException
 PostSharp.Laos.OnFieldAccessAspect
 OnGetValue
 OnSetValue
Other Aspects
 PostSharp.Laos.ImplementMethodAspect
 Replace a method’s content with the advice in the aspect
 Useful for modifying 3rd party components
 PostSharp.Laos.CompositionAspect
 Allows an interface/state to be injected into a
 Used to simulate multiple inheritance
 Examples include
Adding .NET Win Form data binding to a POCO
Adding Entity Framework interfaces to a POCO
 PostSharp - David Ross
 Introduction to AOP with PostSharp - Michael D. Hall
 What Is PostSharp? -
 Using AOP and PostSharp to Enhance Your Code: Part
A - Doron's .NET Space
 PostSharp - Lightweight Aspect-Oriented System
Learn more...