aop with post sharp

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

Upload: lance-zhang

Post on 28-Nov-2014

5.244 views

Category:

Technology


0 download

DESCRIPTION

 

TRANSCRIPT

Page 1: Aop With Post Sharp

AOP with PostSharp

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

http://blodfox777.cnblogs.com

I am not an AOP expert.

Page 2: Aop With Post Sharp

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

Page 3: Aop With Post Sharp

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

Page 4: Aop With Post Sharp

Application ConcernsFunctional Requirements

Line-of-business.Non-Functional Concerns

LoggingCachingTransactionSecurityValidationPersistence…

Page 5: Aop With Post Sharp

Enter the Aspect.

What is AOP?

Page 6: Aop With Post Sharp

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: Aop With Post Sharp

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: Aop With Post Sharp

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: Aop With Post Sharp

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: Aop With Post Sharp

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: Aop With Post Sharp

Enter the PostSharp

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

Page 12: Aop With Post Sharp

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: Aop With Post Sharp

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

Page 14: Aop With Post Sharp

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

Page 15: Aop With Post Sharp

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: Aop With Post Sharp

Assembly Before/After Injection Before InjectionAtfer Injection

Page 17: Aop With Post Sharp

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: Aop With Post Sharp

When Can PostSharp Insert Code?

PostSharp.Laos.OnMethodBoundaryAspectOnEntryOnExitOnSuccessOnException

PostSharp.Laos.OnMethodInvocationAspectOnInvocation

PostSharp.Laos.OnExceptionAspectOnException

PostSharp.Laos.OnFieldAccessAspectOnGetValueOnSetValue

Page 19: Aop With Post Sharp

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: Aop With Post Sharp

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: Aop With Post Sharp

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: Aop With Post Sharp

Thank You!&

Any Questions?