Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / AOP

Unity 2.0 Interception via Method Decoration in verbose code

0.00/5 (No votes)
24 Jul 2012CPOL 33.8K  
Unity 2.0 Interception via Method Decoration in verbose code
I found a lot of examples on how to implement Unity interception with configuration files, but no concise examples of how to configure it with verbose code. Hopefully someone else will find this useful too.
 
C#
using Microsoft.Practices.Unity;
using Microsoft.Practices.Unity.InterceptionExtension;
using System;

namespace UnityExample
{
    class Program
    {
        //Private Variable
        private static IUnityContainer unityContainer;

        static void Main(string[] args)
        {
            unityContainer = new UnityContainer();

            //This allows Interception for the whole Unity container.
            unityContainer.AddNewExtension<interception>();

            //This sets up an InterfaceInterceptor for IAuthenticationService
            //IAuthenticationService is a class I wrote (see below), simply substitute  
            //whatever class you're interested in intercepting here
            unityContainer.Configure<interception>()
               .SetInterceptorFor<iauthenticationservice>(new InterfaceInterceptor());
        }
    }

    //define what it is you want to do with your custom interceptor
    public class LogExceptionsHandler : ICallHandler
    {
        public int Order { get; set; }
        public IMethodReturn Invoke(IMethodInvocation input, GetNextHandlerDelegate getNext)
        {   //Put any logic here you would like to invoke BEFORE the method is invoked.
            Console.WriteLine("Before method Invocation happened");

            //This invokes the next item in the Injection pipeline, or eventually
            //calls your method
            var methodReturn = getNext().Invoke(input, getNext);

            // AFTER the target method execution check to see if it excepted 
            if (methodReturn.Exception != null)
            {
                Console.WriteLine("Add some real logging here");
            }

            //if you would like to re-throw, do it here, otherwise just return 
            //your methodReturn
            else
            {
                Console.WriteLine
                   ("To Rethrow, or not to rethrow, that is the question.");
            }
            return methodReturn;
        }
    }

    //create an attribute so you can apply it to your methods
    public class LogExceptionsAttribute : HandlerAttribute
    {
        public override ICallHandler CreateHandler(Microsoft.Practices.Unity.IUnityContainer container)
        {
            return new LogExceptionsHandler();
        }
    }

    //lastly, apply the decoration to the method you want to intercept
    //Note: This is the same Interface defined above in the .SetInterceptorFor
    public interface IAuthenticationService
    {
        [LogExceptions]
        bool isAuthenticated(string userName, string password);
    }
}

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)