Introduction

In this article, I've used PostSharp which is one of the most friendly tools to weave codes at compile time in order to develop a few non-functionals like Exception Handling, Logging, Execution Timing, and Transaction Management. It allows using AOP in your C# code with a very simple and intuitive syntax. I have seen that many developers repeat aforementioned blocks of code throughout their application and consequently their code is difficult to understand and to change. In addition, AOP complements OOP and in order to remove intermingled codes which appear in every class, AOP is recommended strongly. I've demonstrated how to use AOP to reduce lines of code and create programs which are easy to read and manage.

Note: Remember to set the TestConnectionString in the app.config and run the Script.Sql in your system prior to running the sample code.

Requirements

To follow along with this article, you will need the following installed:

Visual Studio 2010 or higher and .NET 4.0 or higher. Visual Studio Express will allow for PostSharp’s post-compilation support so the Aspects you write will work, but the PostSharp IDE extensions are not available for Express.

Background

In AOP, there are a few terms which are essential to know. Defining these terms are more difficult than implementing them. However, I will define them briefly and their responsibility will be clarified later in sample code. I'll discuss the most important ones.

1. Crosscutting Concerns

Referred to intermingled codes which every class contains. For instance, in the following code, Exception Handling, and Tracing blocks are called intermingled codes and can be moved into an aspect.

2. Aspect

Aspects will alter behavior to other classes or objects during compile time or run time. They are called either weaving a class or object. In our examples, which will be discussed later, aspects are defined as attributes and will be declared on top of the methods or classes to add some functionality to those methods or classes.

3. Joinpoint

We can consider every point in our code in which another method is called as a Joinpoint.

4. Pointcut

A way of defining Joinpoints in our code. Pointcut also contains an advice that is to occur when the joinpoint is reached. So if we define a Pointcut on a particular method being invoked, when the invocation occurs or the joinpoint is invoked, it is intercepted by the AOP framework and the pointcut's advice is executed.

5. Advice

In our code, when we reach a Joinpoint, we will invoke another method or methods. These method(s) are considered as Advices.

Using the Code

Our sample is a C# Console application. As can be seen from the following picture, we need to add a reference to PostSharp.Dll. In addition, There are four different aspects (in Aspects folder) in our code which can be seen below:

First of all, I'll discuss Timing Aspect. In many applications, it is important for developers or users to calculate execution time of a piece of code. In a traditional method, we used to use the following code to calculate such a value:

But in every application, we may have to repeat such a code throughout our application in order to get elapsed time in different methods. AOP will assist us to diminish such codes. In this scenario, we will create an aspect and use it as an attribute on top of every method for which its execution time is needed, as below:

TimingAspect inherits from the OnMethodBoundaryAspect type. That type, explained in far greater detail in the extensive online PostSharp documentation, provides opportunities to intercept the code of a method and to execute code prior to it, after it, on success, or on failure only. OnEntry, OnExit are our Advices which will be invoked when LongRunningCalc is invoked and finished. In this case, the point in code where LongRunningCalc is invoked, is a JoinPoint. OnEntry is invoked when LongRunningCalc is invoked by PostSharp and OnExit is invoked when the execution of LongRunningCalc finished. Here is the result of LongRunningCalc execution:

LogAspect is quite similar to TimingAspect as it inherited from the OnMethodBoundaryAspect and override OnEntry, OnExit as below:

RunInTransactionAspect aspect inherits from the OnMethodBoundaryAspect but in order to support Transaction management, we have to implement OnEntry, <code><code>OnExit, OnSuccess, and OnException. When an exception is thrown by a JointPoint, the whole transaction will be rolled back and potential data problems averted. otherwise the transaction will be completed. We need to handle this situations in OnException, and OnSuccess methods respectively.

Note: In the above code, AspectTypeDependency attribute has been used to inform PostSharp to run RunInTransactionAspect after LogAspect everywhere they are used together.
Here is our main code and the result of its execution:

Note: In the above code, commented codes are codes which are omitted as they have been replaced with aspects.

Conclusion

PostSharp is one of the most popular means of implementing AOP within the .NET Framework. When applied appropriately, PostSharp Aspects can reduce code clutter and help to maintain architectural standards and practices without overcomplicating the code with intermingled responsibilities. In this example, PostSharp has allowed the refactoring of one application so that:

Logging code has been extracted into a separate Aspect and removed from the code explicitly

Performance-analysis code has been extracted into an Aspect and is no longer intermingled

Transactional processing has been extracted and removed from the code, cleaning up database execution code

can you please help me to fix why comments in cs ( timing aspect.cs and other) using postsharp is not reflecting. in case of exception it giving error rather then proper out put. i have to comment all below due to diff exception and while in case of LongRunningCalc() it is not showing comment defined in timingaspect.cs. do i need to install something to make it work?

Hi,
Well one guess is that it actually depends how you run your application. On debug mode or on release mode as Trace object is meaningful mostly in release mode. Take a look at your Output window, for sure there must be something at least.

Hi, it is my pleasure you liked it. Actually, I have not encountered such a problem. However, I googled and I found this thread which is related to Postsharp(obviously Postsharp is making this problem):http://www.sharpcrafters.com/forum/Topic6135-20-1.aspx[^]
Here guys with similar problem have come up with some solutions

i change the [Disable PostSharp] to [YES] in Project's Property & i Compile it Successfully .
i'm looking for a better Solution to compile my project with PostSharp
if not found , I'll Reinstall it . thanks Man