Get Rid of Boilerplate Code with AOP

Recently, I decided that I was sick to death of writing DNN exception handling code. It is the same thing written over and over and over again

Typical Boilerplate code:

protected void SomeEventHandler(Object sender, EventArgs e)
		// Do something that might throw an exception
	catch (Exception ex)
		Exceptions.ProcessModuleLoadException(this, ex);

This might seem incredibly lazy but I have written that exact try catch block thousands (if not millions) of times.

This alone is enough to make me want to find an alternative but what if I wanted to change the way I handle exceptions? What if a client wanted me to send them an email every time an exception is thrown for example?

I would have to change every single try catch in the entire application!

Aspect Oriented Programming using PostSharp

To achieve this task I utilized a library called PostSharp. In short, it provides you the hooks to add in logic and code when certain things happen.

PostSharp is the #1 pattern-aware extension to C# and VB. It allows developers to eradicate boilerplate by offloading repeating work from humans to machines. PostSharp contains ready-made implementations of the most common patterns and gives you the tools to build automation for your own patterns.

Taken from the docs!

First we need to write a little class to handle when an exception is thrown:

    public class HandleDnnException : OnExceptionAspect
        /// <summary>
        /// Types of exceptions handled by HandleDnnException Aspect
        /// </summary>
        public enum DnnExceptionType

        /// <summary>
        /// The Exception action type. Default is ProcessModuleLoadException
        /// </summary>
        public DnnExceptionType ExceptionType { get; set; } = DnnExceptionType.ProcessModuleLoad;

        public override void OnException(MethodExecutionArgs args)
            // An exception has been thrown
            switch (ExceptionType)
                case DnnExceptionType.ProcessModuleLoad:
                    ProcessModuleLoadException(args.Instance as Control, args.Exception);

There is now one place to maintain our exception handler logic. I give the option of processing the exceptions in 2 different ways.

Now, if we needed to add some extra logic we only have to add it in here!

You can find the rest of the code on BitBucket or use it directly in your project by installing the nuget package

Now, we can handle exceptions with a single attribute on our methods:

	protected override void OnLoad(EventArgs e)
		// Any exceptions thrown in here will be handled! No boiler plate!


Other resources:

AOP with PostSharp


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s