Presentation is loading. Please wait.

Presentation is loading. Please wait.

Aspect Oriented Programming (AOP) in.NET Brent Krueger 12/20/13.

Similar presentations


Presentation on theme: "Aspect Oriented Programming (AOP) in.NET Brent Krueger 12/20/13."— Presentation transcript:

1 Aspect Oriented Programming (AOP) in.NET Brent Krueger 12/20/13

2  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.  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. What is AOP?

3  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 pointcut could be “before I run any method”, “after I run any method”, or something more complicated like “when I run any method in this namespace”. Cross Cutting Concerns, Advice and Pointcuts

4  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 You might already be using AOP

5  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. With/Without AOP

6 Weaving Example

7  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. Method Interception

8

9  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. Boundary Aspects

10 Where do boundaries exist?

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

12  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. Interception Locations: Lazy Loading

13 Lazy Loading Approach in.NET with/without AOP

14  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. AOP Implementation Types

15 Run-time Weaving using Proxy Pattern

16 Compile Team Weaving

17  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. Aspect Composition


Download ppt "Aspect Oriented Programming (AOP) in.NET Brent Krueger 12/20/13."

Similar presentations


Ads by Google