Wednesday, February 02, 2005

Aspect Oriented .NET

On another of my projects I am looking at ways to help automate commonly repeated tasks. For example. In this project, all exceptions within the service layer are managed and wrapped in a custom exception. The Exception Management application block is used to publish the exceptions details. Each of the exceptions has an associated Publishable Exception, which contains the exception to be thrown beyond the service layer. This ensures that only appropriate exceptions are made public.

In order to implement this all public methods in the service layer must catch all unhandled exceptions and then throw the associated Publishable Exception. For example the code is something like this:

public void ServiceLayerMethod(Parameters p)
{
try
{
...
}
catch(Exception exc)
{
Manager.RaiseFilteredException(exc);
}
}

While this is not much code it is annoying to type, and a bigger problem if missed.

Getting this out of the code and into some form of aspect, or even attribute, has been a goal of mine for a while. This can be achieved by using ContextBound objects, but this forces the developer to inherit from this class and is not as flexible as I would like. Today I have spend some time looking into some of the other Aspect Oriented extensions for .NET. This has lead me to XC# from ResolveCorp. I have only just download the VS2005 version and will investigate it over the next few days.

Has anyone tried using this? The documentation on the site is very thin... but it does appear that I should be able to implement this as an aspect. If this is the case the plan is to create a ExceptionManaged attribute that can be applied at a class or method level. At a class level, all non-private methods will be wrapped with the exception management code.

If you have had any experience with XC# that you think will help please let me know... if not I will post up my progress as I go.

No comments: