.NET CodeCop Interfaces Pipelined Interception

Intercepting Miss Interface

Today I want to talk about one of the nicest features of CodeCop which is Pipelined Interception.

Simply putting, Pipelined Interception is the ability to intercept methods on interfaces (generic or not) and subsequently on all types that implement them.

What this means is that if you have a set of types, all implementing the same interface you just need to target your interceptor(s) at that common interface instead of having to configure them per type basis.

Let’s see a quick example of just that by creating a Console Application project in Visual Studio (framework 4.0 and onwards).

Add the CodeCop nuget package to it:
install-package codecop

I will now add 3 new classes to the project, Class1, Class2 and Class3 respectively and an interface named IFoo.

The interface code just looks like this:

 public interface IFoo
    {
        void Method1();
        void Method2();

    }

All 3 classes will implement this interface like so:

  public class Class1 : IFoo
    {
        public void Method1()
        {
           Console.WriteLine("Class1.IFoo.Method1()");
        }

        public void Method2()
        {
            Console.WriteLine("Class1.IFoo.Method2()");

        }
    }
  public class Class2 : IFoo
    {
        public void Method1()
        {
           Console.WriteLine("Class2.IFoo.Method1()");
        }

        public void Method2()
        {
            Console.WriteLine("Class2.IFoo.Method2()");

        }
    }
  public class Class3 : IFoo
    {
        public void Method1()
        {
           Console.WriteLine("Class3.IFoo.Method1()");
        }

        public void Method2()
        {
            Console.WriteLine("Class3.IFoo.Method2()");

        }
    }

Now we need an interceptor. For the sake of brevity I will create it in the same project, but as a best practice it is advisable to always create your interceptors in different assemblies to avoid coupling them with the intercepted code.

I called it InterfaceInterceptor and it’s a boundary interceptor because it only intercepts the method’s execution “borders”, before and after (CodeCop can also override method behaviors completely, refer to the project wiki for details).

The interceptor code is very simplistic and looks like this:

  public void OnBeforeExecute(InterceptionContext context)
        {
            Console.WriteLine("Before {0}.IFoo.{1}()", context.Sender.GetType().Name, context.InterceptedMethod.Name);
        }

        public void OnAfterExecute(InterceptionContext context)
        {
            Console.WriteLine("After {0}.IFoo.{1}()", context.Sender.GetType().Name, context.InterceptedMethod.Name);
            Console.WriteLine(Environment.NewLine);
        }

Add a new JSON file to the project, call it copconfig.json (mandatory name) and select Copy if newer on the Copy To Output Directory file properties in Visual Studio.

I will not use the reverse engineering tool that CodeCop provides us to create the JSON because this is a simple example that I can “cook” manually. This is how the JSON looks like:

{
    "Types": [
        {
            "TypeName": "PipelinedExample.IFoo, PipelinedExample",
            "Methods": [
                {
                    "MethodSignature": "*",
                    "Interceptors": [ "InterfaceInterceptor"]
                }
            ],
           "GenericArgumentTypes": []

        }

    ],
    "GlobalInterceptors": [],
    "Key":"Paste your product key here if you have one (limited to intercepting 25 methods on Free mode)"
}

Under the Program class, we bootstrap CodeCop by calling its Intercept method and add a call to each interface method of Class1, Class2 and Class3.

  class Program
    {
        static void Main(string[] args)
        {
            Cop.Intercept();
            
            new Class1().Method1();
            new Class1().Method2();
            
            new Class2().Method1();
            new Class2().Method2();
            
            new Class3().Method1();
            new Class3().Method2();
        }
    }

Run the app and behold the result:
console-out

As you can see, all we had to do was to specify the interface on the JSON and automatically CodeCop applied to Class1, Class2 and Class3 the InterfaceInterceptor interceptor. Beautiful right?

I’m sure you got the whole picture now. This is a huge time-saving feature, especially on large enterprise applications where can be dozens of types implementing the same interface and all of them targeted for interception.

I want to finish today’s post by saying that if you want to know more about method interception, how you can use it to better craft your code, then subscribe to the 5-Day Free Crash Course To Method Interception that is available on the product homepage. All subscribers will be getting a 10% discount on all product licenses, so don’t miss this time-bounded opportunity.

Until next time, where I will showcase some ready-to-use interceptors and how you can start building and sharing those within the CodeCop user Community.

Free Email Updates
Get the latest content first.
100% Privacy. No spam.
Liked it? Please Share.

Leave a Reply

Your email address will not be published. Required fields are marked *