Unity and the Validation Appliction Block

Oct 28, 2008 at 11:39 AM
I am trying to get the latest drop of Unity and a the Validation Application Block to play nicely together but I am having a few issues.

I want the Unity to intercept CRUD methods on my DAL class. i have created an attribute [EnforceValidation] that is placed on each of my crud methods I want intercepted. The purpose of the [EnforceValidation] attribute is to force the object that is about to be persisted to the database to be validated before it is persisted.

ie

public class ActiveRecordDomainAdapter<T> : IActiveRecordDomainAdapter<T> where T : DomainBase<T>
    {
         [EnforceValidation]
        public void Create(T instance)
        {
            ActiveRecordMediator<T>.Create(instance);
        }


}

the container is configured via code.

ServiceLocator.Current.AddNewExtension<Interception>();
            ServiceLocator.Current.RegisterType(typeof(IActiveRecordDomainAdapter<>),
                                                typeof(ActiveRecordDomainAdapter<>))
                                                .Configure<Interception>().SetInterceptorFor(typeof(IActiveRecordDomainAdapter<>),
                                                new InterfaceInterceptor());

The EnforceValidation attribute code is as follows.

public class EnforceValidationAttribute : HandlerAttribute
    {
        public override ICallHandler CreateHandler(IUnityContainer container)
        {
            return new EnforceValidationHandler();
        }
    }

    public class EnforceValidationHandler : ICallHandler
    {
        #region Implementation of ICallHandler

        public IMethodReturn Invoke(IMethodInvocation input, GetNextHandlerDelegate getNext)
        {
            ValidationManager.Validate(input.Arguments[0]);
            return getNext()(input, getNext);
        }

        public int Order
        {
            get; set;
        }
        #endregion
    }

Where the ValidationManager is a wrapper around the Ent Lib Validation class.

The problem is that the ent lib var validationResults = Validation.Validate(item); code returns bogus results when called with in the EnforceValidationHandler

If I call the validation code before the crud method is invoked it works correctly. If I call the validation code inside the crud method it works correctly.

Any ideas????????

Also I noticed the PIAB has a [ValidationCallHandler] attribute should I be using this and configuring it via unity and if so how?

Am I missing the point altogether and is there a better way to do this?????

Thanks in advance
Adam






Nov 3, 2008 at 7:43 AM
I think you could simplify this.  Take a look at this screencast about integrating VAB with Unity:  http://www.pnpguidance.net/Screencast/EnterpriseLibrary4ValidationApplicationBlockUnityIoCScreencast.aspx. 


- Sarah