ncsoft china pd dept. training - session 4 -- lance zhang
TRANSCRIPT
AOP with PostSharp
NCSoft China PD Dept. training - Session 4-- Lance Zhang
http://blodfox777.cnblogs.com
I am not an AOP expert.
TopicsWhat is AOPExisting AOP Frameworks in .NETIntroduce PostSharp How does PostSharp workHow to use PostSharpQ & A
Suppose We have a System…It Grows…Code Smells similar…Now We've Got A Problem(Ctrl + C/V)…So We Concerns…
Application ConcernsFunctional Requirements
Line-of-business.Non-Functional Concerns
LoggingCachingTransactionSecurityValidationPersistence…
Aspects - Reduce noise in sourceMove plumbing out of source, but keep
behaviour the sameAOP …
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 LingoAdviceThe duplicated plumbing code we are removingTypically under 30 lines of codeBehaviour that is injected at a join point
Join pointsPlaces within the code where the Aspect is insertedExamples
Entry/Exit of a method or propertyClass’s Type Definition
Aspects Lingo
Point cutLocates Join Points to apply adviceFilter driven – Automatic injection
Find all Methods, of type Setter, in all classes where Namespace equals “Application.Entities”
Attribute driven – Manual injection
Aspects Lingo
WeavingProcess of injecting functionality back into a
componentCan be performed by
Text post processor – Magic comments can replaced by code
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 ProxyDynamic Proxy does the heavy-lifting of the IL
generation for usMuch easier for most requirements
AOP with Spring.NETCan use something other than attributes such
as XML configuration.
Sorry, but actually I am unfamiliar
with them…
Enter the PostSharp
What is PostSharp?•PostSharp is a Post-Compiler •PostSharp is an Aspect-Oriented Framework
AOP with PostSharpCompile-time MSIL Injection - by MSBuild Task Easy to use - like normal Attribute Intercept any method - not only VirtualMore Join points - on: field access, exceptionMore controllability - break, change param or
returnMore 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 write plumbing inline. Then we refector by PostSharp.
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.
How Can I Implement PostSharp?
PostSharp.Laos.dllHandles 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 framework.
PostSharp.Public.dllSupport library for PostSharp.
PostSharp.AspNet.dllOnly if you are in an ASP.NET project.
When Can PostSharp Insert Code?
PostSharp.Laos.OnMethodBoundaryAspectOnEntryOnExitOnSuccessOnException
PostSharp.Laos.OnMethodInvocationAspectOnInvocation
PostSharp.Laos.OnExceptionAspectOnException
PostSharp.Laos.OnFieldAccessAspectOnGetValueOnSetValue
Other AspectsPostSharp.Laos.ImplementMethodAspect
Replace a method’s content with the advice in the aspect
Useful for modifying 3rd party components
PostSharp.Laos.CompositionAspectAllows an interface/state to be injected into a
componentUsed to simulate multiple inheritanceExamples include
Adding .NET Win Form data binding to a POCO Adding Entity Framework interfaces to a POCO
ReferencesPostSharp - David RossIntroduction to AOP with PostSharp - Michael
D. HallWhat Is PostSharp? -
http://www.postsharp.orgUsing AOP and PostSharp to Enhance Your
Code: Part A - Doron's .NET SpacePostSharp - Lightweight Aspect-Oriented
System - http://www.rainsts.net/
Learn more...http://www.postsharp.org/http://davybrion.com/blog/category/
postsharp/http://www.codeplex.com/ValidationAspectshttp://www.eclipse.org/aspectj/doc/released/
progguide/