Aspect Oriented Programming (AOP) has been around for a long time now. It enables you to decouple the cross cutting concerns from your application and solve code tangling and eliminate code duplication. Some of the widely used cross-cutting concerns include: logging, exception handling, security, caching, performance monitoring, etc. There are many AOP frameworks around and AspectJ is one of the popular AOP frameworks. It should be noted that Aspect.NET is the .NET counterpart of this popular AOP framework that enables a clean modularization of the crosscutting concerns in your application. It is an open source .NET multi-language aspect weaver. The Aspect.NET framework is an AOP framework for Microsoft .NET. In this article we will explore the advantages of AOP and learn how we can implement AOP and leverage its benefits.
Aspect .NET Concepts
The three main concepts related to Aspect .NET include the following:
- Join points — this refers to a point in your application where an aspect can be applied. Typical examples of join points include: a method call point, an exception being thrown, etc.
- Pointcuts — this refers to the joinpoint in your application's code where the associated advice should be applied. In AOP, a set of joinpoints is known as a pointcut.
- Advice — this is used to define the action that should be performed at a particular joinpoint. In other words, it refers to the action that is to be taken at a particular joinpoint.
As I said, AOP extends OOP to provide you a way to write code that is much more modular and maintainable. In other words, Aspect Oriented Programming is an extension of Object Oriented Programming enabling you to write code that is much more maintainable. Note that AOP doesn’t replace OOP in any way, rather it complements OOP. Aspects are actually reusable software modules that encapsulate functionalities that include: error handling, exception handling, logging, configuration management, security, etc.
Weaving in Aspect Oriented Programming
Note that Aspect.NET takes advantage of two strategies to work with aspects and AOP. These include AOP meta-language and attribute specifications. The act of injecting the AOP code into our application is known as weaving. There are a few different ways in which you can achieve this. These include the following:
- Compile-time weaving — this is a type of weaving that occurs at compile-time and hence the name. In this type of weaving, the cross-cutting concerns in your application's code is weaved before the MSIL or IL code is generated.
- Binary weaving — Post compile time weaving or binary weaving is a type of weaving in which the aspects are injected into already compiled code, i.e., your MSIL or the IL code.
- Load-time weaving — this is a type of weaving that is performed at runtime. Hence, this process is also known as runtime weaving. This is a process that injects the aspects to the compiled MSIL or IL code during class loading. This type of weaving is achieved by taking advantage of the .NET runtime environment.
Programming Aspect .NET
Now, let's get into a bit of code. The following code snippet illustrates how you can create a custom aspect class using this framework.
[AspectDescription("This is a custom aspect")]
public class CustomAspect : Aspect
The following two method illustrate how you can take advantage of AspectAction attribute to inject aspects before and after a method call.
[AspectAction("%before %call *")]
public static void BeforeCall()
DisplayMessage("Hello " + TargetMemberInfo.ToString());
[AspectAction("%after %call *")]
public static void AfterCall()
DisplayMessage("Bye " + TargetMemberInfo.ToString());
The following code snippet shows how the DisplayMessage method looks like.
private static void DisplayMessage(String str)
This article presented an introduction of AOP, its concepts and how it can be implemented using the Aspect.NET framework.