Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Aop With Post Sharp

4,812 views

Published on

Published in: Technology
  • Be the first to comment

  • Be the first to like this

Aop With Post Sharp

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

×