1 / 17

Aspect Oriented Programming (AOP) in .NET

Aspect Oriented Programming (AOP) in .NET. Brent Krueger 12/20/13. What is AOP?. Term first originated in 1997, in a research paper by Gregor Kiczales of Xerox. His team was concerned about the repetition of boilerplate code that was often necessary in large scale OOP projects.

karlyn
Download Presentation

Aspect Oriented Programming (AOP) in .NET

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. Aspect Oriented Programming (AOP)in .NET Brent Krueger 12/20/13

  2. What is AOP? • Term first originated in 1997, in a research paper by GregorKiczales of Xerox. • His team was concerned about the repetition of boilerplate code that was often necessary in large scale OOP projects. • Common examples were logging, caching, and transacting. • AOP involves using “Aspects” that encapsulate cross-cutting concerns and allow reuse of code. • This white paper lead to the creation of AspectJ, which is still the leading AOP tool used in the Java world today.

  3. Cross Cutting Concerns, Advice and Pointcuts • Cross Cutting Concerns: Pieces of functionality used across multiple parts of a system. • Common examples: Logging, caching, transacting. • Advice: The code that performs the cross cutting concern. • Advice is the “What?” of AOP. • Pointcut: A “seam” in execution of our application. This is the “Where?” of AOP. • An example of a pointcutcould be “before I run any method”, “after I run any method”, or something more complicated like “when I run any method in this namespace”.

  4. You might already be using AOP • ASP.NET forms authentication • IHttpModule • ASP.NET MVC Authentication • IActionFilter • Creating new pages doesn’t require re-implementing or duplicating authentication code. • ORM Tools with Lazy Loading like nHibernate

  5. With/Without AOP Without using AOP, we often mix core concerns together with cross cutting concerns within a method, causing “tangling” or “spaghetti code”. When cross cutting code is used in multiple methods and multiple classes, this is called “scattering”. We want “weaving”, which allows separation of cross cutting concerns and core logic. Allowing your AOP tool to apply aspects to your business classes.

  6. Weaving Example

  7. Method Interception • Method Interception is an aspect that runs a piece of code in place of some other method. • This sounds like a bad idea, but can actually help with decoupling in your code. • An example would be a method that sends out a tweet. • We could validate the text, or perform a replace. • We can log whether or not the tweet was successful. • Allows modifying behavior of a method without changing one line of code in the method itself.

  8. Method Interception

  9. Boundary Aspects Method interception involves code that runs in-place of other code. Boundary aspects involve running code around code that it is bounding. Real-life example: Detecting mobile users or caching. Present users with an option to install a native application when on a mobile device. Similar to how we might check for authentication on a request.

  10. Where do boundaries exist?

  11. Intercepting Locations A “Location” in C# is referring to a field or property. Real World Example: Notified on property changes or lazy loading.

  12. Interception Locations: Lazy Loading The purpose of lazy loading is to defer execution of a costly operation for as long as possible. Used in ORM tools like nHibernate Makes use of Location Interception to allow first time access to a property to cause initialization. Subsequent uses of this property will return the initialized value.

  13. Lazy Loading Approach in .NET with/without AOP

  14. AOP Implementation Types Runtime Weaving: Makes use of the proxy pattern to create a proxy of implemented class. Compile-time weaving: AOP tool kicks in during compilation to add its appropriate code for aspects where needed.

  15. Run-time Weaving using Proxy Pattern

  16. Compile Team Weaving

  17. Aspect Composition • You can use composition to make aspects even more powerful. • Specify an aspect role that relies on other aspects. Forcing certain aspects to be used together. • Specify ordering of aspects applied.

More Related