Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

Dependency Injection in C# Using Aspect.NET Framework

Take advantage of the Aspect.NET Framework, the .NET multi-language aspect weaver, to inject dependencies in your application.


advertisement

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:

  1. 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.
  2. 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.
  3. 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.

Aspects

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.



using AspectDotNet;
namespace DevX.Aspects
{
 [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)
  {
      Console.WriteLine(str);
  }

Summary

This article presented an introduction of AOP, its concepts and how it can be implemented using the Aspect.NET framework.



   
Joydip Kanjilal is a Microsoft Most Valuable Professional in ASP.Net, as well as a speaker and the author of several books and articles. He received the prestigious MVP (Most Valuable Professional) award at ASP.Net for 2007, 2008, 2009, 2010, 2011, and 2012. He has more than 18 years of industry experience in IT, with more than 14 years in Microsoft .Net and its related technologies. He has been selected as MSDN Featured Developer of the Fortnight (MSDN) and as Community Credit Winner several times. He is the author of eight books and more than 300 articles. He was a speaker a speaker at the reputed Spark IT 2010 event and at the reputed Dr. Dobb’s Conference 2014 in Bangalore. He is also a regular speaker at SSWUG Virtual Conference. He's also worked as a judge for the Jolt Awards at Dr. Dobb's Journal.
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date