NCSoft China PD Dept. training - Session 4 -- Lance Zhang http://blodfox777.cnblogs.com Topics What is AOP Existing AOP Frameworks in .NET Introduce PostSharp How does PostSharp work How to use PostSharp Q&A Suppose We have a System… It Grows… Code Smells similar… Now We've Got A Problem(Ctrl + C/V)… So We Concerns… Application Concerns Functional Requirements Line-of-business. Non-Functional Concerns Logging Caching Transaction Security Validation Persistence … 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. 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 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 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 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… 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 That's Great, But I Need Some Code. Originally we writeby plumbing inline. Then we refector PostSharp . Okay, Where Is The Plumbing Code? Attribute Exception handling Attributefor for CachingSharp . 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. Assembly Before/After Injection Before Injection Atfer Injection 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. 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 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 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/ Learn more... http://www.postsharp.org/ http://davybrion.com/blog/category/postsharp/ http://www.codeplex.com/ValidationAspects http://www.eclipse.org/aspectj/doc/released/progguid e/