Top Banner
AOP with PostSharp NCSoft China PD Dept. training - Session 4 -- Lance Zhang http://blodfox777.cnblogs.com I am not an AOP expert.
22

NCSoft China PD Dept. training - Session 4 -- Lance Zhang .

Dec 16, 2015

Download

Documents

Lucy Converse
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: NCSoft China PD Dept. training - Session 4 -- Lance Zhang .

AOP with PostSharp

NCSoft China PD Dept. training - Session 4-- Lance Zhang

http://blodfox777.cnblogs.com

I am not an AOP expert.

Page 2: NCSoft China PD Dept. training - Session 4 -- Lance Zhang .

TopicsWhat is AOPExisting AOP Frameworks in .NETIntroduce PostSharp How does PostSharp workHow to use PostSharpQ & A

Page 3: NCSoft China PD Dept. training - Session 4 -- Lance Zhang .

Suppose We have a System…It Grows…Code Smells similar…Now We've Got A Problem(Ctrl + C/V)…So We Concerns…

Page 4: NCSoft China PD Dept. training - Session 4 -- Lance Zhang .

Application ConcernsFunctional Requirements

Line-of-business.Non-Functional Concerns

LoggingCachingTransactionSecurityValidationPersistence…

Page 5: NCSoft China PD Dept. training - Session 4 -- Lance Zhang .

Enter the Aspect.

What is AOP?

Page 6: NCSoft China PD Dept. training - Session 4 -- Lance Zhang .

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.

Page 7: NCSoft China PD Dept. training - Session 4 -- Lance Zhang .

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

Page 8: NCSoft China PD Dept. training - Session 4 -- Lance Zhang .

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

Page 9: NCSoft China PD Dept. training - Session 4 -- Lance Zhang .

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

Page 10: NCSoft China PD Dept. training - Session 4 -- Lance Zhang .

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…

Page 11: NCSoft China PD Dept. training - Session 4 -- Lance Zhang .

Enter the PostSharp

What is PostSharp?•PostSharp is a Post-Compiler •PostSharp is an Aspect-Oriented Framework

Page 12: NCSoft China PD Dept. training - Session 4 -- Lance Zhang .

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

Page 13: NCSoft China PD Dept. training - Session 4 -- Lance Zhang .

That's Great, But I Need Some Code. Originally we write plumbing inline. Then we refector by PostSharp. 

Page 14: NCSoft China PD Dept. training - Session 4 -- Lance Zhang .

Okay, Where Is The Plumbing Code? Attribute for Exception handlingAttribute for CachingSharp. 

Page 15: NCSoft China PD Dept. training - Session 4 -- Lance Zhang .

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.

Page 16: NCSoft China PD Dept. training - Session 4 -- Lance Zhang .

Assembly Before/After Injection Before InjectionAtfer Injection

Page 17: NCSoft China PD Dept. training - Session 4 -- Lance Zhang .

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.

Page 18: NCSoft China PD Dept. training - Session 4 -- Lance Zhang .

When Can PostSharp Insert Code?

PostSharp.Laos.OnMethodBoundaryAspectOnEntryOnExitOnSuccessOnException

PostSharp.Laos.OnMethodInvocationAspectOnInvocation

PostSharp.Laos.OnExceptionAspectOnException

PostSharp.Laos.OnFieldAccessAspectOnGetValueOnSetValue

Page 19: NCSoft China PD Dept. training - Session 4 -- Lance Zhang .

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

Page 20: NCSoft China PD Dept. training - Session 4 -- Lance Zhang .

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/

Page 21: NCSoft China PD Dept. training - Session 4 -- Lance Zhang .

Learn more...http://www.postsharp.org/http://davybrion.com/blog/category/

postsharp/http://www.codeplex.com/ValidationAspectshttp://www.eclipse.org/aspectj/doc/released/

progguide/

Page 22: NCSoft China PD Dept. training - Session 4 -- Lance Zhang .

Thank You!&

Any Questions?