Skip to content

Action Interception Aspect

Sagi edited this page Apr 9, 2015 · 12 revisions

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

using NCop.Mixins.Framework;
using NCop.Composite.Framework;

[TransientComposite]
[Mixins(typeof(CSharpDeveloperMixin))]
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 ActionInterceptionAspect.

public class StopwatchActionInterceptionAspect : ActionInterceptionAspect
{
    private readonly Stopwatch stopwatch = null;

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

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

using NCop.Mixins.Framework;
using NCop.Composite.Framework;

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

public class StopwatchActionInterceptionAspect : ActionInterceptionAspect<string>
{
}

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

  1. OnInvoke - Invoked instead of the method to which the aspect has been applied.

Each code piece is called advice. Let's write some code on the OnInvoke advice.

public class StopwatchActionInterceptionAspect : ActionInterceptionAspect
{
    private readonly Stopwatch stopwatch = null;

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

    public override void OnInvoke(ActionInterceptionArgs args) {
        stopwatch.Restart();
        args.Proceed();
        stopwatch.Stop();
        Console.WriteLine("Elapsed Ticks: {0}", stopwatch.ElapsedTicks);
    }
}

In order for NCop to apply StopwatchActionInterceptionAspect as an aspect we need to annotate the Code subroutine with MethodInterceptionAspectAttribute attribute.

using NCop.Mixins.Framework;
using NCop.Composite.Framework;
using NCop.Aspects.Framework;

[TransientComposite]
[Mixins(typeof(CSharpDeveloperMixin))]
public interface IDeveloper
{
    [MethodInterceptionAspect(typeof(StopwatchActionInterceptionAspect))]
    void Code();
}

An aspect can be placed also on the Mixin's subroutine.

public class CSharpDeveloperMixin : IDeveloper
{
    [MethodInterceptionAspect(typeof(StopwatchActionInterceptionAspect))]
    public void Code() {
        Console.WriteLine("C# coding");
    }
}

The last thing that we have to do is create a CompositeContainer which will handle two things:

  1. Craft the real implementation in runtime.
  2. Act as a Dependency Injection Container that will resolve our type.
using System;
using NCop.Mixins.Framework;
using NCop.Composite.Framework;
using NCop.Aspects.Framework;

class Program
{
    static void Main(string[] args) {
        IDeveloper developer = null;
        var container = new CompositeContainer();

        container.Configure();
        developer = container.Resolve<IDeveloper>();
        developer.Code();
    }
}

The expected output should be:
"C# coding"
"Elapsed Ticks: [Number of ticks]"
Your end result of the code should be similar to this:

using System;
using NCop.Aspects.Framework;
using NCop.Mixins.Framework;
using NCop.Composite.Framework;
using System.Diagnostics;

namespace NCop.Samples
{
    [TransientComposite]
    [Mixins(typeof(CSharpDeveloperMixin))]
    public interface IDeveloper
    {
        [MethodInterceptionAspect(typeof(StopwatchActionInterceptionAspect))]
        void Code();
    }

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

    public class StopwatchActionInterceptionAspect : ActionInterceptionAspect
    {
        private readonly Stopwatch stopwatch = null;

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

        public override void OnInvoke(ActionInterceptionArgs args) {
            stopwatch.Restart();
            args.Proceed();
            stopwatch.Stop();
            Console.WriteLine("Elapsed Ticks: {0}", stopwatch.ElapsedTicks);
        }
    }

    class Program
    {
        static void Main(string[] args) {
            IDeveloper developer = null;
            var container = new CompositeContainer();

            container.Configure();
            developer = container.Resolve<IDeveloper>();
            developer.Code();
        }
    }
}