PostSharp now supports Silverlight 2!

header While I was over in Poland a few weeks (or is it months now?) ago I met Gael Fraiteur, creator of the awesome Open Source .NET AOP container called PostSharp.  We had a great talk about development, trends, history and music, as I recall, but one topic we spent a bit of time talking about was Silverlight.  We both agreed that Silverlight was an incredibly cool, enabling and liberating technology for .NET developers, and he mentioned that he was anxious to get some updates to PostSharp to support Silverlight 2.  I’m excited to help him announce that PostSharp now supports adding aspects to your Silverlight 2  applications

Here are a couple of samples that Gael provided to me that I wanted to share with you that shows how these aspects would work. 

Example 1

The following custom attribute, when applied on a field, checks that it is neither empty neither null:

    1:  public class RequiredAttribute : OnFieldAccessAspect
    2:  {
    4:      public override void OnSetValue(FieldAccessEventArgs eventArgs)
    5:      {
    6:          string s;
    7:          if (eventArgs.ExposedFieldValue == null ||
    8:              (  (s = eventArgs.ExposedFieldValue as string) != null)
    9:                  && s.Length == 0)
   10:          {
   11:              throw new ArgumentNullException(
   12:                  eventArgs.FieldInfo.Name,
   13:                  "Cannot set this field to null or empty.");
   14:          }
   16:          base.OnSetValue(eventArgs);
   18:     }
   20:  }

Here is how it is applied to the Person class:

    1:  public class Person
    2:  {
    4:      [Required] private string firstName;
    5:      [Required] private string lastName;
    7:      public string FirstName
    8:      {
    9:          get { return firstName; }
   10:          set { firstName = value; }
   11:      } 
   13:      public string LastName
   14:      {
   15:          get { return lastName; }
   16:          set { lastName = value; }
   17:      }
   19:  }


When the application is compiled, PostSharp uses the code injection method of weaving aspects to take these attributes and turn them into executable directives inside your .NET assemblies.  If your application code does not supply values for FirstName and LastName, then the error condition would be met and the ArgumentNullException will be raised.

Example 2

This custom attribute, when applied an a method this time, ensures that the method is always executed on a background thread:

    1:  public class OnWorkerThreadAttribute : OnMethodInvocationAspect
    2:  {
    4:      public override void OnInvocation(MethodInvocationEventArgs eventArgs)
    5:      {
    6:          ThreadPool.QueueUserWorkItem(state => eventArgs.Proceed());
    7:      }
    9:  }
   11:  [OnWorkerThread]
   12:  private void somethingLongButton_Click(object sender, System.Windows.RoutedEventArgs e)
   13:  {
   14:      // do work
   15:  }


Using an AOP container like PostSharp can help simplify your application by reducing the overall number of lines of code and improving logical decoupling in your application logic by separating frequently reused code into attributes. Additionally, since PostSharp works at the IL level, it “supports virtually all static languages targeting the .NET framework”. 

There have been a number of AOP frameworks created for .NET over the years, but I highly encourage you to check out PostSharp.  For more information, please visit the following web sites:

PostSharp Web Site:

PostSharp RSS feed:

.NET Rocks! Episode featuring Gael and PostSharp:

Technorati Tags: .net,aop,aspectorientedprogramming,postsharp