Wiki

Clone wiki

CodeCop / Use Cases

Use Cases

This page is where we highlight some of the real world scenarios where CodeCop might come in handy. Bear in mind that these examples represent only a few of the unlimited usage possiblities for this product.

Instrumentation

Have you ever found yourself in a situation where you needed to monitor or measure the level of a product's performance in order to diagnose errors or to write trace information to a database or file system log? If so then we're sure you'll agree that 99% of the times you "polluted" every method to monitor with lots of tracing code and repeated that process for an indefinite number of times as the total number of methods. Right? Ouch!

Here's a painkiller for you. With CodeCop you just have to write a single Interceptor:

#!c#

 public class InstrumentationInterceptor : ICopIntercept
    {
       public InstrumentationInterceptor (ILogService service)
       {
         // Do your interceptor ctor logic here 
       }

        public void BeforeExecute(InterceptionContext context)
        {
            // Do your instrumentation logic here 
        }

        public void AfterExecute(InterceptionContext context)
        {
            // Do your instrumentation logic here
        }

    }

You don't have to touch your code anymore. Via a JSON configuration file you specify which are the types and respective methods where this interceptor applies.

#!json

{
    "Types": [
        {
            "TypeName": "ConsoleApplication1.MyClass, ConsoleApplication1",
            "Methods": ["*"],
        },
        {
            "TypeName": "ConsoleApplication1.AnotherClass, ConsoleApplication1",
            "Methods": [
                {
                    "MethodName": "Foo",
                    "Interceptors": [ ]
                },
                  {
                    "MethodName": "Bar",
                    "Interceptors": [ ]
                }
            ],
           "GenericArgumentTypes": [ ]
        }

    ],
    "GlobalInterceptors": ["InstrumentationInterceptor"]

}

Boilerplate Code

It's very frequent that the first lines of code inside a method are to check if its parameters are null and throw an ArgumentNullException if that is true. A single interceptor that performs this logic is only what you need:

#!c#
// This interceptor surrounds every call to a proxied method with code that checks for null parameters
public class GuardNullInterceptor : Interceptor
{
  public void OnBeforeExecute(Interception context)
  {
     foreach (var parameter in context.Parameters)
     {
        if(parameter.Value == null)
        {
          throw new ArgumentNullException(parameter.Name);
        }        
     }    
  }
}

Fakes/Mocking

With CodeCop you can surrogate the entire method execution logic. It's so easy to mock or fake any behaviour you want when the method is called:

#!c#

// Instead of running the original method code, other faked code is executed
public object OnOverride(InterceptionContext context)
{
    return new FakedObject();
}

Attribute Based Logic

Interceptors make natural replacers for attributes. They don't need to be spreaded along the code and they can be added or removed via configuration.

Take for instance this Interceptor example that adds a new HTTP header after a method call:

#!c#

public class CustomHttpHeaderInterceptor : Interceptor
{
  public void AfterExecute(InterceptionContext context)
  {
    HttpContext.Current.Response.AppendHeader("SomeHeaderName", "SomeHeaderValue");
  }
}

Updated