3

From Clean Code, about Cross-Cutting Concerns:

Note that concerns like persistence tend to cut across the natural object boundaries of a domain. You want to persist all your objects using generally the same strategy, for example, using a particular DBMS...

Is Active Record an example of aspect-oriented programming?

In AOP, modular constructs called aspects specify which points in the system should have their behavior modified in some consistent way to support a particular concern. This specification is done using a succinct declarative or programmatic mechanism.

If Active Record is an example of AOP, what is the "aspect"? Is it the class declaration that inherits from Active Record?

class Foo < ActiveRecord::Base

1 Answer 1

3

No, inheritance can't really handle cross cutting concerns - when they talk about cross cutting they basically mean multiple object hierarchies are the things being cross cut.

Aspect Oriented Programming mostly is used to talk about some sort of extension to a single inheritance language to handle cross cutting concerns. It's a bit less useful to add AOP to a multiple inheritance language, because multiple inheritance can handle some of the problems AOP solves (though, depending on AOP implementation, it might allow things like adding implementation to code from a third party module).

Ruby doesn't have multiple inheritance, but it does have mixins, and that can also solve some of the problems AOP does.

However, AOP can still do some other things as well. Think of logging as a cross cutting concern. With AOP you can add logging to classes without touching the source of the original class. This is done by the AOP implementation finding all the places in the code that match your criteria for where logging should occur, and patching in ("weaving") calls to do the logging.

This is also possible in Ruby without special AOP extensions, because it's so very dynamic. You could modify a class (that previously had no notion of logging) at run time to call logging code at the time any of it's methods are run. The code that you wrote to do this would be handling a cross-cutting concern. The "aspect" would be logging. In Ruby the classes you do this to need not even be classes you wrote. So this is more than can be done via single or multiple inheritance or mixins (though, to be a bit pedantic, you can do aspect oriented things and propagate the code that implements it via inheritance or mixins, those are not language features sufficiently powerful in themselves to allow AOP).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.