Archive

AOP for .NET

AOP on .NET

These pages are about Aspect-Oriented Programming (AOP) techniques for the .NET Framework. They help you understand what all is AOP and how AOP is implemented on .NET. It is PostSharp-centric, but not limited to PostSharp. We always try to refer to other existing AOP tools when relevant.

What is Aspect-Oriented Programming (AOP)?

Aspect-Oriented Programming (AOP), also named Aspect-Oriented Software Development (AOSD), is an approach to software development that goes further in the direction of separation of concerns. Separation of concerns is one of the most important rules in software development. It states that the same concern should be solved in a single unit of code. This is also called modularization. In procedural programming, the unit of code is the procedure (or function, or method). In object-oriented programming, the unit of code is the class.

Some concerns cannot be implemented successfully using a pure procedural or object-oriented programming. An example is code security. If you want to secure objects and methods, you have to modify the code of each method. That's why security is said a crosscutting concern, because it crosscuts the unit of modularization of the programming paradigm, in this case the class.

An aspect is a concern that cross-cuts many classes and/or methods.

So AOP is a technique that allows to address issues that cross-cuts objects. AOP is frequently used to implement caching, tracing, security or failure injections.

See also our Glossary for technical terms.

Example

Say we have a large set of business objects and some of their methods need to be secured. The current user is required to be in some roles in order to be given the permission to execute the method. Using the AOP programming model, you could, instead of modifying each method, develop an aspect and 'apply' it on methods of interest. In PostSharp Laos, the aspect could look like this:

public sealed class RequiresRoleAttribute : OnMethodBoundaryAspect
{
  string[] roles;
  public string[] Roles 
  { 
    get { return this.roles; }
    set { this.roles = value; }
  }

  public override void OnEntry( MethodExecutionEventArgs e )
  {
    ((ISecurable) e.Instance).RequireRoles( this.roles );
  } 
}

This defines a custom attribute that can be applied to any method. You can use wildcards to apply the aspect to a set of objects and methods:

[assembly: RequiresRole( 
      Roles=new string[] { "Delete" }, TargetMethods="Delete*" )]

There are of course a lot of AOP techniques to achieve the same result. The illustration above is only one of them.

Major AOP techniques in .NET

There are basically two approaches to change the behavior of a program in .NET (i.e. to weave a .NET program):

  • Compile-Time Weaving: the program is modified during the build process on the development machine, before deployment.
  • Runtime Weaving: the program is modified during its execution, after deployment.

Expressing the semantics

We know what we want: apply additional behaviors to existing code. Now we need to know how to express it. We have to express the following facts:

  • What behavior we want to add (advices),
  • Where we want to add it (specification of join points using pointcuts).

The following approaches are available when the .NET Framework is targeted:

  • Extending the Language : Create a new .NET language with new constructs.
  • Companion Files : Use external files, for instance XML files.
  • Custom Attributes : Use 'normal' .NET custom attributes to annotate the code.
  • Domain-Specific Languages: Use of a graphical designer to express the semantics, as well as IDE integration to simplify build and debugging experiences
  • Programmatic Tipping : All previous approaches were declarative. Another solution is to have normal .NET imperative code to add advices to join points.

Commercial Licenses

 Commercial Licenses are offered by Coding Glove, the company behind PostSharp.

Open Source License

Run-time components of PostSharp (PostSharp.Public.dll, PostSharp.Laos.dll) are released under GNU Lesser General Public License (LGPL) version 3, but compile-time parts (all other libraries) are released under the GNU General Public License (GPL) version 3.  

Therefore, if you use PostSharp to make a program for end-users, you don't need to release it as open source. However, if your product uses PostSharp to modify programs of your customers (which means that you would typically need to include PostSharp.Core.dll in your distribution package), you have either to publish it as open source, or to buy a commercial license.

Note that, by exception to GPL, you may use link PostSharp compile-time to modules released under any OSI-approved license.

In other words:

  • Programs transformed by PostSharp are never subject to license restrictions because they are linked only to LGPL components, which is not a viral license.
  • Aspect libraries written for PostSharp Laos are not subject to license restriction. However, if you develop new abstract aspect with their own weaver (i.e. you typically reference PostSharp.Laos.Weaver.dll), you are subject to license restrictions.
  • If you develop a plug-in to PostSharp, or make a program using PostSharp low-level APIes, you should release it under GPL or one of the other supported open-source licenses.

Open-Source License Agreement for PostSharp

THE SOFTWARE IS LICENSED UNDER THIS AGREEMENT (“OPEN-SOURCE LICENSE AGREEMENT FOR POSTSHARP”) OR THE COMMERCIAL LICENSE AGREEMENT FOR POSTSHARP. YOU, AS AN INDIVIDUAL OR A LEGAL ENTITY, CAN USE THE SOFTWARE UNDER ONE OF THESE LICENSE AGREEMENTS ONLY. IF YOU USE THE SOFTWARE UNDER THE COMMERCIAL LICENSE AGREEMENT, USING THE SOFTWARE UNDER THE OPEN-SOURCE LICENSE AGREEMENT WOULD BE AN INFRINGEMENT TO THE COMMERCIAL LICENSE AGREEMENT.

 1. Parties

(a) "Licensor" means Gael Fraiteur, having its principal place of business at Lutovitova 932, Kralupy nad Vltavou, 27801, Czech Republic.

(b) "Licensee" means any individual or organization using the Program.

2. Definitions

(a) “Program” means software program known as PostSharp in binary or source code form, but not including its installer package, user documentation, and samples.

(b) “Public Components” means the libraries PostSharp.Public and PostSharp.Laos.

(c) “Open-Source Software” is any program or library released under a license recognized by the Open Source Initiative.

(d) “Closed-Source Software” is any program or library that is not Open-Source Software.

3. Licensing of Public Components

Public Components are free software; you can redistribute them and/or modify them under the terms of the GNU Lesser General Public License, version 3.

Linking other modules to run-time libraries of PostSharp does not affect licensing terms of these modules. However, some other requirements apply. See the GNU Lesser General Public License for more details.

4. Licensing of the Program

The Program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License, version 3.

4.1. Interpretation of “Covered Work”

Linking other modules statically or dynamically, directly or indirectly, with compile-time libraries of the Program is making a combined work based on the Program. Thus, the terms and conditions of the GNU General Public License cover the whole combination.

“Linking” is shall not be understood in a technical meaning, but in a functional meaning: if a component requires the Program to fulfill its function, it is considered as being linked to it.

“Linking” is transitive: if a component denoted A is linked to another component denoted B, and component B is linked to the Program, it is considered that component A is linked to the Program.

4.2. Additional Permissions

In addition, as a special exception to the GPL v3, the Licensor gives you permission to combine the Program with any Open-Source Software. You may copy and distribute an Open-Source Software combined with the Program following the terms of the GNU GPL and the licenses of the other code concerned, provided that you include the source code of that other code when and as the GNU GPL requires distribution of source code.

Note that people who make modified versions of the Program are not obligated to grant these special exceptions for their modified versions; it is their choice whether to do so. The GNU General Public License gives permission to release a modified version without this exception; this exception also makes it possible to release a modified version which carries forward this exception.

4.3. Additional Restrictions

(a) If you convey a modified copy of the covered work, you must clearly specify the origin of the material.

(b) Under trademark laws, if you convey a modified copy of the covered work, you cannot use the word 'PostSharp' in the name of your new product.

5. Licensing of the Installer Package and the User Documentation

The Licensor grants the Licensee an unlimited permission to use the unmodified Installer Package and User Documentation.

You may convey verbatim copies of the Installer Package or User Documentation as you receive it, in any medium, provided that you conspicuously and appropriately publish on each copy an appropriate copyright notice; keep intact all notices stating that this License apply to the program; keep intact all notices of the absence of any warranty; and give all recipients a copy of this License along with the software.

6. Disclaimer of Warranty.

THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM “AS IS” WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION.

7. Limitation of Liability.

IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.