Aop With Post Sharp

4,403
-1

Published on

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
4,403
On Slideshare
0
From Embeds
0
Number of Embeds
4
Actions
Shares
0
Downloads
54
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

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 />
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×