Skip to content

Function Interception Aspect

Sagi edited this page Apr 9, 2015 · 9 revisions

Let's change a bit the type that we've created in the Atom Composite example.
We will change the method from a subroutine (method that does not returns a value) to a function.

public interface IDeveloper
{
    /// From
    void Code();
}

public interface IDeveloper
{
    /// To
    string Code();
}

Let's implement the new Mixin.

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

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

public class CSharpDeveloperMixin : IDeveloper
{
    public string Code() {
        return "C# coding";
    }
}

We want to apply the aspect on the Code function.
The function has a string return value, therefore we need to create a new aspect that is derived from FunctionInterceptionAspect<string>

public class StopwatchFunctionInterceptionAspect : FunctionInterceptionAspect<string>
{
    private readonly Stopwatch stopwatch = null;

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

If the Code function accepted one parameter of type string then our aspect would derive from `FunctionInterceptionAspect<string, string>

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

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

public class StopwatchFunctionInterceptionAspect : FunctionInterceptionAspect<string, 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 StopwatchFunctionInterceptionAspect : FunctionInterceptionAspect<string>
{
    private readonly Stopwatch stopwatch = null;

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

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

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

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

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

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

public class CSharpDeveloperMixin : IDeveloper
{
    [OnMethodBoundaryAspect(typeof(StopwatchFunctionInterceptionAspect))]
    public string Code() {
        return "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) {
        string result = null;
        IDeveloper developer = null;
        var container = new CompositeContainer();

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

The expected output should be:
"Elapsed Ticks: [Number of ticks]"
"C# coding"
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(StopwatchFunctionInterceptionAspect))]
        string Code();
    }

    public class CSharpDeveloperMixin : IDeveloper
    {
        public string Code() {
            return "C# coding";
        }
    }

    public class StopwatchFunctionInterceptionAspect : FunctionInterceptionAspect<string>
    {
        private readonly Stopwatch stopwatch = null;

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

        public override void OnInvoke(FunctionInterceptionArgs<string> 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>();
            Console.WriteLine(developer.Code());
        }
    }
}