BOSS
BOSS

Reputation: 2951

What is implemetation difference between dojo.aspect and dojox.lang.aspect

While implementating aspect oriented programming i am getting confuse Why Dojo having to two differentaspect library files?

When to use

dojo.aspect and dojox.lang.aspect ?

Upvotes: 0

Views: 661

Answers (2)

Philippe
Philippe

Reputation: 6828

From a usage standpoint, dojo/aspect is a very simplified version of dojox/lang/aspect.
With dojo/aspect, you can create aspects corresponding to a named function (e.g. the "get" method in the "xhr" class), allowing you to create a before, after or around advice anytime xhr.get is called.

On the other hand, (TMHO) only dojox/lang/aspect provides enough features to play with aop. It allows you to define your pointcuts with regular expressions, therefore allowing things like "execute an around advice for any functions whose name starts with get on any object"...

You can even pass-in an array of function names or regular expressions to which your aspects will be applied.

The blog post pointed by phusick gives good examples of that.

Upvotes: 2

phusick
phusick

Reputation: 7352

I have never heard about dojox.lang.aspect before, but according to git log the latest commit dates to 2008.

You can find an explanation why dojox.lang.aspect exists in an article by its author Eugene Lazutkin: AOP aspect of JavaScript with Dojo.

While it doesn't make much sense in some cases, we should realize that the primary role of dojo.connect() is to process DOM events.

[...]

To sum it up: events != AOP. Events cannot simulate all aspects of AOP, and AOP cannot be used in lieu of events.

So AOP in JavaScript sounds simple! Well, in reality there are several sticky places:

  • Implementing every single advice as a function is expensive.
    • Usually iterations are more efficient than recursive calls.
    • The size of call stack is limited.
    • If we have chained calls, it'll be impossible to rearrange them. So if we want to remove one advice in the middle, we are out of luck.
    • The “around” advice will “eat” all previously attached “before” and “after” advices changing the order of their execution. We cannot guarantee anymore that “before” advices run before all “around” advices, and so on.
  • Usually in order to decouple an “around” advice from the original function, the proceed() function is used. Calling it will result in calling the next-in-chain around advice method or the original method.
  • Our aspect is an object but in some cases we want it to be a static object, in other cases we want it to be a dynamic object created taking into account the state of the object we operate upon.

These and some other problems were resolved in dojox.lang.aspect (currently in the trunk, will be released in Dojo 1.2).

As of the latest Dojo 1.7, there is a strong tendency to differentiate between events and aspects, i.e. between dojo/on and dojo/aspect (both were implemented via dojo.connect before).

Upvotes: 3

Related Questions