Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations

Presentation on theme: "NCSoft China PD Dept. training - Session 4 -- Lance Zhang"— Presentation transcript:

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

2 Topics What is AOP Existing AOP Frameworks in.NET Introduce PostSharp How does PostSharp work How to use PostSharp Q & A

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

4 Application Concerns Functional Requirements Line-of-business. Non-Functional Concerns Logging Caching Transaction Security Validation Persistence …


6 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.

7 Aspects Lingo Advice 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

8 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

9 Aspects Lingo Weaving Process of injecting functionality back into a component Can 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

10 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 configuration. Sorry, but actually I am unfamiliar with them…


12 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

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

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

15 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.

16 Assembly Before/After Injection Before Injection Atfer Injection

17 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 framework. PostSharp.Public.dll Support library for PostSharp. PostSharp.AspNet.dll Only if you are in an ASP.NET project.

18 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

19 Other Aspects PostSharp.Laos.ImplementMethodAspect Replace a method’s content with the advice in the aspect Useful for modifying 3 rd party components PostSharp.Laos.CompositionAspect Allows an interface/state to be injected into a component Used to simulate multiple inheritance Examples include Adding.NET Win Form data binding to a POCO Adding Entity Framework interfaces to a POCO

20 References PostSharp - David Ross Introduction to AOP with PostSharp - Michael D. Hall What Is PostSharp? - http://www.postsharp.org Using AOP and PostSharp to Enhance Your Code: Part A - Doron's.NET Space PostSharp - Lightweight Aspect-Oriented System -

21 Learn more... e/


Download ppt "NCSoft China PD Dept. training - Session 4 -- Lance Zhang"

Similar presentations

Ads by Google