Enterprise Library 6, Unity 3, MVC, Validation with Interception ValidationCallHandler Part 5

This post continues on from the previous post. Part 4 Enterprise Library 6, Unity 3 InterfaceInterceptor with MVC 4

In this post a validation call handler will be added using unity 3 with VirtualMethodInterceptor and enteprise library Policy Injection. Enterprise library 6 has the following call handlers (Out of the box):

  1. AuthorizationCallHandler
  2. ExceptionCallHandler
  3. LogCallHandler
  4. PerformanceCounterCallHandler
  5. ValidationCallHandler

Using the ValidationCallHandler attribute, any method can be extended with a declaration decorator. In a first step a enterprise library validation ruleset will be defined directly in the method.

To start add the nuget pacakges:
ValidationNuget

Both the EL6 Validation block and the EL6 Policy Injection blocks are required.

Now a new DiagnosisCallHandler is required

using System;
using Microsoft.Practices.Unity.InterceptionExtension;
using MvcUnityBootstrapperTest.Logging;

namespace MvcUnityBootstrapperTest.UnityExtensions
{
    public class DiagnosisCallHandler : ICallHandler
    {
        public IMethodReturn Invoke(IMethodInvocation input, GetNextHandlerDelegate getNext)
        {
            DiagnosisEvents.Log.MethodEnter(String.Format("[{0}:{1}]", this.GetType().Name, "Invoke"));

            // BEFORE the target method execution
            DiagnosisEvents.Log.LogVerboseMessage(String.Format("{0} {1}", input.MethodBase.ToString(), input.Target.ToString()));

            // Yield to the next module in the pipeline
            var methodReturn = getNext().Invoke(input, getNext);

            // AFTER the target method execution
            if (methodReturn.Exception == null)
            {
                DiagnosisEvents.Log.MethodLeave(String.Format("Successfully finished {0} {1}", input.MethodBase.ToString(), input.Target.ToString()));
            }
            else
            {
                DiagnosisEvents.Log.MethodLeave(String.Format("Finished {0} with exception {1}: {2}", input.MethodBase.ToString(), methodReturn.Exception.GetType().Name, methodReturn.Exception.Message));
            }

            return methodReturn;
        }

        public int Order
        {
            get;
            set;
        }
    }
}

And also a attribute

using Microsoft.Practices.Unity;
using Microsoft.Practices.Unity.InterceptionExtension;

namespace MvcUnityBootstrapperTest.UnityExtensions
{
    public class DiagnosisHandlerAttribute : HandlerAttribute
    {
        public override ICallHandler CreateHandler(IUnityContainer container)
        {
            return new DiagnosisCallHandler();
        }
    }
}

Now a method ist required to be injected. NOTE: The virtual is required because VirtualMethodInterceptor is used for the injection. The ruleset is direct in the method.


using Microsoft.Practices.EnterpriseLibrary.Validation.PolicyInjection;
using Microsoft.Practices.EnterpriseLibrary.Validation.Validators;

[ValidationCallHandler()]
[DiagnosisHandlerAttribute()]
public virtual void Deposit(
[RangeValidator(typeof(Decimal), 
"1.0", RangeBoundaryType.Exclusive, 
"2.0", RangeBoundaryType.Exclusive)] decimal depositAmount)
{
    string x = "";
}

Now the unity configuration method is extended.

 container.Configure<Interception>().SetInterceptorFor<UnitOfWorkExample>(
                     new VirtualMethodInterceptor());

code: https://github.com/damienbod/MvcUnityValidationHandlerInterception.git

The deposit method is executed as follows:
injectionCycle

As you see it is relatively simple to add validation using unity 3 interception.

Part 1 Enterprise Library 6, Unity 3 with ASP.NET MVC 4
Part 2 Enterprise Library 6, Unity 3 and MVC 4, LifetimeManagers
Part 3 Enterprise Library 6, Unity 3 and MVC 4, Registration by Convention
Part 4 Enterprise Library 6, Unity 3 InterfaceInterceptor with MVC 4

Links

http://unity.codeplex.com/

http://entlib.codeplex.com/

Leave a Reply

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

WordPress.com Logo

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

Facebook photo

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

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: