Click here to Skip to main content
15,891,704 members
Articles / Web Development / ASP.NET

ASP.NET MVC controller action with Interceptor pattern

Rate me:
Please Sign up or sign in to vote.
4.40/5 (4 votes)
2 Oct 2012CPOL8 min read 67.8K   924   24  
This article is to demonstrate interceptor pattern with MVC controller action, and so action can be intercepted in controller classes without using action filters.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using MvcCallInterceptors.Controllers;
using System.Reflection;
using System.Linq.Expressions;

namespace MvcCallInterceptors.Interceptors
{
    internal sealed class InterceptMethodDispatcher
    {

        private delegate object InterceptorExecutor(IInterceptorMvcController controller, InterceptorParasDictionary<string,object> parameters, object result);
        private delegate void VoidInterceptorExecutor(IInterceptorMvcController controller, InterceptorParasDictionary<string, object> parameters, object result);

        private InterceptorExecutor _executor;       
        public InterceptMethodDispatcher(MethodInfo methodInfo)
        {
            _executor = GetExecutor(methodInfo);            
            MethodInfo = methodInfo;
        }

        public MethodInfo MethodInfo
        {
            get;
            private set;
        }

        public object Execute(IInterceptorMvcController controller, InterceptorParasDictionary<string, object> parameters, object result)
        {
            return _executor(controller, parameters, result);
        }

        private static InterceptorExecutor GetExecutor(MethodInfo methodInfo)
        {
            // Parameters to executor
            ParameterExpression controllerParameter = Expression.Parameter(typeof(IInterceptorMvcController), "controller");
            ParameterExpression parametersParameter = Expression.Parameter(typeof(InterceptorParasDictionary<string, object>), "parameters");
            ParameterExpression resultParameter = Expression.Parameter(typeof(object), "result");

            // Build parameter list
            List<Expression> parameters = new List<Expression>();
            ParameterInfo[] paramInfos = methodInfo.GetParameters();

            ParameterInfo paramInfo0 = paramInfos[0];            
            UnaryExpression valueCast0 = Expression.Convert(parametersParameter, paramInfo0.ParameterType);
            parameters.Add(valueCast0);

            ParameterInfo paramInfo1 = paramInfos[1];
            UnaryExpression valueCast1 = Expression.Convert(resultParameter, paramInfo1.ParameterType);
            parameters.Add(valueCast1);
            
            // Call method
            UnaryExpression instanceCast = (!methodInfo.IsStatic) ? Expression.Convert(controllerParameter, methodInfo.ReflectedType) : null;
            MethodCallExpression methodCall = methodCall = Expression.Call(instanceCast, methodInfo, parameters);

            // methodCall is "((TController) controller) method((T0) parameters[0], (T1) parameters[1], ...)"
            // Create function
            if (methodCall.Type == typeof(void))
            {
                Expression<VoidInterceptorExecutor> lambda = Expression.Lambda<VoidInterceptorExecutor>(methodCall, controllerParameter, parametersParameter, resultParameter);
                VoidInterceptorExecutor voidExecutor = lambda.Compile();
                return WrapVoidInterceptorCall(voidExecutor);
            }
            else
            {
                // must coerce methodCall to match InterceptorExecutor signature
                UnaryExpression castMethodCall = Expression.Convert(methodCall, typeof(object));
                Expression<InterceptorExecutor> lambda = Expression.Lambda<InterceptorExecutor>(castMethodCall, controllerParameter, parametersParameter, resultParameter);
                return lambda.Compile();
            }
        }

        private static InterceptorExecutor WrapVoidInterceptorCall(VoidInterceptorExecutor executor)
        {
            return delegate(IInterceptorMvcController controller, InterceptorParasDictionary<string, object> parameters, object result)
            {
                executor(controller, parameters, result);
                return null;
            };
        }

    }

}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

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


Written By
Technical Lead
India India
Like to dream on long drive and falling asleep on short drive..!

Comments and Discussions