1 / 22

AOP with PostSharp

AOP with PostSharp. NCSoft China PD Dept. training - Session 4 -- Lance Zhang http://blodfox777.cnblogs.com. I am not an AOP expert. Topics. What is AOP Existing AOP Frameworks in .NET Introduce PostSharp  How does PostSharp work How to use PostSharp Q & A.

tadhg
Télécharger la présentation

AOP with PostSharp

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

  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 • …

  5. Enter the Aspect. What is AOP?

  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…

  11. Enter the PostSharp What is PostSharp? • PostSharp is a Post-Compiler • PostSharp is an Aspect-Oriented Framework

  12. AOP with PostSharp • Compile-timeMSIL 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.  • Then we refector by PostSharp.  • Originally we write plumbing inline. 

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

  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  • Atfer Injection • Before 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 3rd 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 - http://www.rainsts.net/

  21. Learn more... • http://www.postsharp.org/ • http://davybrion.com/blog/category/postsharp/ • http://www.codeplex.com/ValidationAspects • http://www.eclipse.org/aspectj/doc/released/progguide/

  22. Thank You!&Any Questions? 

More Related