What is PostSharp?

With PostSharp, you can easily write and apply custom attributes that add new behaviors to your code - tracing, thread management, exception handling, data binding, and much more.

PostSharp is the number one choice for aspect-oriented programming on the Microsoft .NET platform.

PostSharp in Action

1. Write an aspect

Rather than explaining complicated theory, we'll show you. Here are five aspects:

TraceAsyncThread DispatchExceptionCache
public class TraceAttribute : OnMethodBoundaryAspect { public override void OnEntry(MethodExecutionEventArgs eventArgs) { Trace.TraceInformation("Entering {0}.", eventArgs.Method); } public override void OnExit(MethodExecutionEventArgs eventArgs) { Trace.TraceInformation("Leaving {0}.", eventArgs.Method); } }

2. Use the aspect

Once you have aspects, apply them to your existing code. It's easy. Aspects are plain custom attributes. Just annotate relevant methods with the aspect:

[Trace] public void CreateCustomer(int id, string name) { /* ... */ }

You can also apply the aspects to many classes and methods in a single line, without even touching the source code of these classes.

[assembly: Trace( AttributeTargetTypes="MyApp.BusinessProcesses.*", AttributeTargetMemberAttributes = AttributeTargetElements.Public )]

The pain we kill: boilerplate code

The typical software project has two types of requirements: functional and technical.

  • Functional Requirements: the code that achieves the primary objective of the program, as typically found in your list of requirements.

  • Technical Requirements: the supporting code – often implemented as boilerplate – that ensures the proper execution and maintainability of the functional code.

You won't find technical code listed with requirements, but as all experienced developers know, your application will be unstable and unmaintainable without it. Because this technical code crosscuts the functional code at various points, it's not properly isolated in maintainable modules.

For instance, we cannot implement tracing, exception handling, thread synchronization or most other technical requirements without adding redundant code to each of the many methods requiring these behaviors.

The solution: encapsulating technical code in aspects

Think of PostSharp as an extension of Visual Studio and your favorite compiler. With PostSharp:

  1. Technical requirements are encapsulated as aspects - plain C#/VB classes.
  2. Aspects are applied to business objects without invading their source code.
  3. Aspects are injected into business methods.

The ultimate benefit is the reduced cost of development and maintenance. Specific benefits will appeal to developers, architects, managers, and independent software vendors in different ways.

How it works: post-compilation assembly transformation

PostSharp operates at build time: it transforms the output of the compiler and injects aspects into your code.

There is no hack: you can view the generated code in a decompiler, like Redgate Reflector. What you see is exactly what executes.