Dependency Injection in C# Using Aspect.NET Framework

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.


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);  }


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

Share the Post:
Share on facebook
Share on twitter
Share on linkedin


The Latest

technology leadership

Why the World Needs More Technology Leadership

As a fact, technology has touched every single aspect of our lives. And there are some technology giants in today’s world which have been frequently opined to have a strong influence on recent overall technological influence. Moreover, those tech giants have popular technology leaders leading the companies toward achieving greatness.

iOS app development

The Future of iOS App Development: Trends to Watch

When it launched in 2008, the Apple App Store only had 500 apps available. By the first quarter of 2022, the store had about 2.18 million iOS-exclusive apps. Average monthly app releases for the platform reached 34,000 in the first half of 2022, indicating rapid growth in iOS app development.

microsoft careers

Top Careers at Microsoft

Microsoft has gained its position as one of the top companies in the world, and Microsoft careers are flourishing. This multinational company is efficiently developing popular software and computers with other consumer electronics. It is a dream come true for so many people to acquire a high paid, high-prestige job