Create an aspect that transparently proxies a component's method call.

← Design Patterns and Idioms · Ref: Q1115

Extend Aspect and register with 'with aspect of':

  SilentAspect extends Aspect
    default operator ?
  ...
  register ConcreteComp() as AbstractComp with aspect of SilentAspect()

A silent aspect (no overridden advice) passes all calls through to the real component.

See Q1116 for aspects with constructor args. See Q1117 for exception handling.

Example

#!ek9
defines module qa.designpatterns.aspectbasic

  defines component

    Worker as abstract
      doWork()
        <- rtn as String: "AbstractWorker"

    ConcreteWorker is Worker
      override doWork()
        <- rtn as String: "ConcreteWorker"

  defines class

    SilentAspect extends Aspect
      default operator ?

  defines application

    AspectApp
      register ConcreteWorker() as Worker with aspect of SilentAspect()

  defines program

    AspectBasicDemo() with application of AspectApp
      stdout <- Stdout()
      worker as Worker!
      result <- worker.doWork()
      stdout.println(result)

Common mistakes

E50200 — EK9 has no '@'-annotations — '@Aspect' is parsed as an invalid directive; apply an aspect with 'with aspect of AspectClass()' in the register statement. See ek9 -h E50200 for details.

Incorrect:

      @Aspect register ConcreteWorker() as Worker with aspect of SilentAspect()

Correct:

      register ConcreteWorker() as Worker with aspect of SilentAspect()
Other ways to ask this
  • I need to wrap a component with an aspect that passes calls through unchanged
  • In AspectJ I'd define a pointcut. Write the EK9 basic aspect
  • Given a concrete component, register it with a silent aspect proxy
  • Set up basic aspect delegation where calls pass through to the real component

Coming from another language?

AspectJ: @Aspect + @Around. Spring AOP: @Aspect + @Before/@After. Python: decorators. EK9: extends Aspect + 'with aspect of' in registration.

Keywords: AOP, aspect, delegation, proxy, transparent, wrap