AngularAOP v0.1.0

Edit · Dec 4, 2013 · 2 minutes read · AngularJS JavaScript OpenSource Programming

In this blog post I’ll introduce you the new way of annotating services included in AngularAOP v0.1.0.

First, why we need a new way for annotation? In v0.0.1 the annotations were made in services’ bodies. It was something like this:

myApp.factory('AwesomeService', function (execute, d1, d2,...,dn) {
  var api = /* definition of our service api */
  return execute(Logger).onThrowOf(execute(Authorization).before(api, {
    methodPattern: /Special|getArticleById/,
    argsPatterns: [/^user$/, /^[Ii]d(_num)?$/]
  }));
});

This way of annotating services led to coupling between the services and the API of the AngularAOP’s aspect factory service called execute. By this coupling we weren’t able to reuse our services in new projects which don’t use AngularAOP.

v0.1.0 introduces a brand new way of annotation which doesn’t couple your services with AngularAOP at all! Now you can declaratively annotate your services in the config callback of your module. For the annotations is used object literal:

myApp.provider('AwesomeService', function () {
  return {
    $get: function (d1, d2,...,dn) {
      return {
        //API here
      };
    }
  };
});

myApp.config(function ($provide, executeProvider) {
  executeProvider.annotate($provide, {
    ArticlesCollection: {
      jointPoint: 'before',
      advice: 'Authorization',
      methodPattern: /Special|getArticleById/,
      argsPatterns: [/^user$/, /^[Ii]d(_num)?$/]
    }
  });
});

As you see in AwesomeService you don’t have any dependencies which couples you with AngularAOP! Awesome, right? Internally AngularAOP uses $provide.decorator to decorate your target services. May be the only drawback from this approach is that you must define your services as providers because otherwise AngularAOP won’t be able to use them in the config callback, in order to decorate them.