Aspect Oriented Programming Examples

Can someone post an example of Aspect Oriented Programming (AOP) that is not logging?

I looked through several resources, but all the examples are trivial logging. What is this good for?

+43
aop
Nov 28 '08 at 11:34
source share
14 answers

One example that was borrowed directly from this Aspect Oriented Programming: Radical Research in Modularity, a Youtube video , was drawing on the display. In the example, you have a drawing program that consists of points, shapes, etc., And when changes to these objects occur, you need to say that the display is updated. Without dealing with this in one aspect, you end up repeating yourself a bit.

AOP, as I understand it, was created so as not to repeat itself for cross-constraint problems that may have nothing to do with business logic. With aspects, you can divide these aspects into aspects. One example was logging, but there are many different things that you could repeat. It has been developing since then, and this does not apply to aspect-oriented programming, but also aspect-oriented modeling.

More information on aspect-oriented programming can be found from these sources:

+33
Nov 28 '08 at 12:12
source share
— -

Security

  • Enter a code that checks access rights and blocks access.

Friendly error messages for asp.net webcontrols / webparts

  • Entry code that catches exceptions and writes stacktrace when compiled or friendly message when compiling with an error (for example, here: Transparent exception handling for asp.net/MOSS2007 (with code) )

Performance

  • Embed code that sets up primary counters to get an overview of where your application is slow.
+11
Nov 28 '08 at 11:50
source share

Validation:

[NotNull] public string Property1 { get; set; } [Length(Min = 10, Max = 20)] public string Property2 { get; set; } [Regex(Expression = @"[abc]{2}")] public string Property3 { get; set; } 
+6
Nov 28 '08 at 11:51
source share

Undo - I invoke a third-party assembly that supports undo operations. This requires callers to create a cancellation context, call some methods in the assembly, and then destroy the cancellation context. Contexts can be nested. In addition, if the context is created, but remains in an undesirable state that requires a restart of the application.

Usually use undo, I would write something like this

  void foo() { int id = lib.create_undo_context(); try { lib.performsomeaction(); lib.performsomeaction(); lib.performsomeaction(); } finally { lib.destroy_undo_context(id); } } 

with PostSharp I define an attribute called [Undo] that creates a cancellation context when the method starts and destroys it when the method exits (even if an exception is thrown) - so the code looks like this:

  [Undo] void foo() { lib.performsomeaction(); lib.performsomeaction(); lib.performsomeaction(); } 

It's a little harder to implement this than I'm showing, because I'm sure all cancellation contexts are canceled even in cases where nested Undo contexts - but you get the idea.

+6
Dec 20 '08 at 13:44
source share

Implementing the design pattern in Java and AspectJ (Hannemann and Kiczales): http://www.cs.ubc.ca/labs/spl/papers/2002/oopsla02-patterns.pdf

This document shows how some GoF design patterns can be better implemented in Java using AspectJ.

+4
Nov 28 '08 at 12:31
source share

Another classic example (for example, logging) is caching. But other examples are more interesting.

+4
Nov 28 '08 at 20:55
source share

Dependency Injection

Strictly speaking, dependency injection is nothing but a cross-concern. Most dependency injection infrastructures use an attribute-based programming style:

 public class Car:IDisposable { [Inject] public IGearBox Gearbox { get; set; } ... } 

The [Inject] attribute can also be designed as an aspect without any dependence on the external environment.

+4
Jul 17 2018-12-12T00:
source share

Security - checking that users have the appropriate permissions before executing certain methods.

+3
Nov 28 '08 at 11:44
source share

You cannot have multiple inheritance in Java. However, using AOP, you can have "limited" multiple inheritance. Try Google to see some examples.

I also agree with Avid. Hannemann and Kiczales are great for learning the basics of design and getting great examples of AOP.

+3
Nov 28 '08 at 12:55
source share

Checking the state invariant. Since PostSharp 1.5 will come with inheritance aspects, even through interfaces, it will provide many new features.

+2
Nov 28 '08 at 11:46
source share

My photo album uses aspectj for three things:

The first, in particular, was pretty straightforward from google tech talk on AOP . It's about modularity in a different way than most people think. Definitely recommend watching if you are interested in how to use it forever.

+2
Nov 28 '08 at 20:43
source share

I used aspect-oriented programming to implement a keyword search engine. This is more like an experiment, but it shows how AOP can be used for purposes other than logging and tracing.

Mostly:
(i) The user of the engine marks their classes as KeywordSearchable,
(ii) The engine begins to track the creation and destruction of these KeywordSearchable instances,
(iii) The engine extracts keywords from these KeywordSearchable objects,
(iv) Given the objects and keywords, the algorithm performs a search.

You can learn more about this at http://montrealistic.blogspot.com/2011/08/aspect-oriented-implementation-of.html .

+2
Aug 01 2018-11-11T00:
source share

Examples of AOP:

  • Analyzer and evaluator for arithmetic expressions. This could have been programmed using a visitor pattern, but I believe that aspects are a better alternative.
  • A simple text editor in which some management tasks (for example, saving the “File changed” flag and window title) are considered as separate aspects.
  • A data structure for character strings in which strings are represented as trees, so the union and substring can be implemented without copying. To maintain efficiency, trees sometimes need to be rebalanced; balancing code is considered as an aspect.

Suppose you want to write a message inside the methods of your domain model:

Example: logging without AOP:

 namespace Examples\Forum\Domain\Model; class Forum { /** * @Flow\Inject * @var \Examples\Forum\Logger\ApplicationLoggerInterface */ protected $applicationLogger; /** * Delete a forum post and log operation * * @param \Examples\Forum\Domain\Model\Post $post * @return void */ public function deletePost(Post $post) { $this->applicationLogger->log('Removing post ' . $post->getTitle(), LOG_INFO); $this->posts->remove($post); } } 

If you need to do this in many places, registration will become part of your domain model logic. You will have to enter all log-dependent dependencies in your models. Since registration is not something that the domain model should take care of, this is an example of a non-functional requirement and a so-called cross-cutting issue.

With AOP, the code inside your model knows nothing about registration. He just focuses on business logic.

+2
Nov 23 '14 at 18:14
source share

Transaction management.

In my opinion, you do not want the objects that may be part of the transaction to know that they are in the transaction. Using AOP, you can create objects in transactions as needed without objects in a transaction that need to be aware that they are in a transaction or even about the existence of an AOP structure.

+1
Apr 08 2018-10-10T00:
source share



All Articles