Skip to content

Action Boundary Aspect

sagifogel edited this page Mar 28, 2015 · 15 revisions

Let's apply the aspect on the same type that we've created in the Atom Composite example.

using NCop.Composite.Framework;

[TransientComposite]
public interface IDeveloper
{
    void Code();
}

public class CSharpDeveloperMixin : IDeveloper
{
    public void Code() {
        Console.WriteLine("C# coding");
    }
}

We want to apply the aspect on the 'Code' subroutine (it does not returns a value), therefore we need to create a new aspect that is derived from OnActionBoundaryAspect.

public class StopwatchInterceptionAspect : OnActionBoundaryAspect
{
    private readonly Stopwatch stopwatch = null;

    public StopwatchInterceptionAspect() {
        stopwatch = new Stopwatch();
    }
}

If the Code subroutine accepted one parameter of type string then our aspect would derive from OnActionBoundaryAspect<string>

using NCop.Composite.Framework;

[TransientComposite]
public interface IDeveloper
{
    void Code(string language);
}

public class StopwatchInterceptionAspect : OnActionBoundaryAspect<string>
{
}

When you derive from a boundary aspect you'll get the option to insert pieces of code in several points in the program (a.k.a join points):

public class StopwatchInterceptionAspect : OnActionBoundaryAspect<string>
{
    private readonly Stopwatch stopwatch = null;

    public StopwatchInterceptionAspect() {
        stopwatch = new Stopwatch();
    }

    public override void OnEntry(ActionExecutionArgs args) { }

    public override void OnException(ActionExecutionArgs args) { }

    public override void OnExit(ActionExecutionArgs args) { }

    public override void OnSuccess(ActionExecutionArgs args) { }
}
  1. OnEntry - Called before the execution of the method.
  2. OnSuccess - Called when the execution of the methodis done executing and there were no exceptions.
  3. OnException - Called when an unhandled exception has been raised.
  4. OnExit - Called after the execution of the method.