Runtime AOP tools

Object Oriented Programming

Runtime AOP tools create decorator/proxy classes dynamically at runtime. These classes are built by examining the signature of classes/interfaces along with an interception aspect that you’ve written, then generate a decorator class. Typically, these tools are part of IoC containers, as such containers allow you to use these generated classes transparently.

My goal in this article isn’t to comprehensively document and explore these IoC tools, but to show a basic demonstration of their interception capabilities.

Castle Windsor/DynamicProxy
Castle DynamicProxy is the name of the AOP tool that was originally created to work with Castle Windsor (an IoC tool). Castle is a project with a collection of frameworks and tools for building enterprise applications in .NET. I’ve shown you several examples throughout the book of Castle DynamicProxy with StructureMap. The next listing shows how you might use it with Castle Windsor (which isn’t remarkably different from using it with StructureMap, except that there is no need for a ProxyHelper class). The listing is a Console application; I’ve installed Castle Windsor with NuGet (Install-Package Castle.Windsor), which will install Castle.Core automatically.


The entire Castle project umbrella—including Windsor, DynamicProxy, and MonoRail—has been one of the cornerstones of open source tools and frameworks for .NET since 2004. It is a popular and widely used set of tools, with a very active support and development community.

I used StructureMap , and its EnrichWith functionality combined with Castle DynamicProxy is really appreciable. It’s certainly possible to use other AOP tools as well. But StructureMap does not contain any AOP capabilities itself. To quote the StructureMap documentation: “StructureMap will never include its own Aspect Oriented Programming model (the world does not need a new one).”

However, StructureMap does have its own interception capability. Its unique approach to interception is worth discussing in this article. StructureMap intercepts the object itself, instead of generating a decorator class, and allows you to manipulate the object. You do this by implementing the InstanceInterceptor interface, as shown in the next listing. This interface contains one method—Process—that receives the object as an argument, and returns the object, giving you a chance to manipulate, examine, or wrap the object.

getfile (1)

The console output is different than the other example because it is intercepting an object instead of method calls.

You could also use Castle DynamicProxy within an InstanceInterceptor as an alternative to using a ProxyHelper class. I’ve found that it takes roughly the same amount of effort, depending on how heavy your usage of aspects is, whether custom conventions are being used, and so on.

The Unity Interception Extension (installed with NuGet with Install-Package Unity.Interception) is a component that is an extension of Unity (also known as the Unity Application Block). This component gives AOP interception capabilities to the core Unity functionality. Unity is an IoC container created by Microsoft with an emphasis on configurability and extension for building enterprise software. With Unity, aspects implement the IInterceptionBehavior interface. This interface has an Invoke method that is analogous to Castle DynamicProxy’s Intercept method.

There are two other members of this interface. GetRequiredInterfaces returns a list of interfaces that you want the dynamically created proxy object to implement (INotifyPropertyChanged, for instance, would be a good candidate for this). WillExecute is for optimization: if it returns false, then the interceptor will be skipped. The aspect in this listing does not use these capabilities.

getfile (2)

The output, once again to Console, is the same as other examples.

The Unity framework is both open source and backed by Microsoft, and thus it is widely used. It is still under active development, with a .NET 4.5 preview available at the time of writing.

Spring.NET is a framework based on the popular Spring framework for Java. It contains a number of features for building enterprise applications (much like Castle Windsor), including Spring.Core (DI) and Spring.Aop (runtime AOP). Spring.NET tends to use XML for configuration of everything, so that’s what I’m going to use in this appendix, but Spring.CodeConfig allows you to configure in C# instead of XML if you wish.

Install Spring’s AOP functionality with NuGet (Install-Package Spring.Aop). This listing shows an aspect that implements the IMethodInterceptor interface, which just has one member: Invoke.

getfile (3)

In a Console project, the Spring configuration will be in app.config, as in the next listing. In an ASP.NET project, it would be in Web.config instead.

getfile (4)

Besides interception aspects, Spring.NET has boundary aspects (IMethodBeforeAdvice, IAfterReturningAdvice, and IThrowsAdvice). Beyond the simple mapping in this configuration, Spring.NET also has more complex configuration options, including regular expressions and dynamic pointcuts (akin to attribute multicasting as with PostSharp).

AOP in .NET: Practical Aspect-Oriented Programming
By: Matthew D. Groves; Foreword by Phil Haack