Click here to Skip to main content
15,887,596 members
Articles / AOP
Tip/Trick

Unity 2.0 Interception via Method Decoration in verbose code

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
24 Jul 2012CPOL 33.2K   4   3
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)


Written By
Software Developer LandmarkDividend
United States United States
This member doesn't quite have enough reputation to be able to display their biography and homepage.

Comments and Discussions

 
QuestionWhere is code exmaple that I can download Pin
PingPong KingKong31-Mar-13 5:28
PingPong KingKong31-Mar-13 5:28 
QuestionUnity interception example doesn't compile Pin
Daniel Ziegelmiller24-Jul-12 4:32
professionalDaniel Ziegelmiller24-Jul-12 4:32 
GeneralRe: Unity interception example doesn't compile Pin
KevonHoughton24-Jul-12 5:24
KevonHoughton24-Jul-12 5:24 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.