Ordering of Behaviours Applied By Policy

21 views
Skip to first unread message

Nick (ntcoding)

unread,
May 20, 2012, 2:45:48 PM5/20/12
to fubumv...@googlegroups.com
Hey,

I have this policy:

public class RemapFormValuesOnValidationFailurePolicy : IConfigurationAction
	{
		public void Configure(BehaviorGraph graph)
		{
			graph
			.Actions()
			.Where(b => b.InputType() != null)
			.Each(chain => chain.WrapWith(typeof(RemapFailedValidationFormValuesBehaviour<>).MakeGenericType(chain.OutputType().BaseType)));
		}
	}


And it allows me to set some values on the view model. But the problem is, the view model does not show the values when rendered. I'm think this behavior i s executing after the output node. If it is, how can I change the ordering so it occurs in between handler and output node?


Here's the behavior  just in case what I'm saying should in theory work:

	public class RemapFailedValidationFormValuesBehaviour<TInputModel> : BasicBehavior where  TInputModel : class
	{
		private readonly IFubuRequest request;
 
		public RemapFailedValidationFormValuesBehaviour(IFubuRequest request) : base(PartialBehavior.Executes)
		{
			this.request = request;
		}
 
		protected override void afterInsideBehavior()
		{
			FailedValidationInputModel<TInputModel> failedValidationContext = null;
			try
			{
				failedValidationContext = request.Get<FailedValidationInputModel<TInputModel>>();
			}
			catch (FubuException)
			{
				return;
			}
 
			if (failedValidationContext.IsForFailedValidation)
			{
				var viewModel = request.Get<TInputModel>();
 
				MapProperties(failedValidationContext.InputModel, viewModel);
 
				request.Set<TInputModel>(viewModel);
			}
		}
 
		private void MapProperties(object from, object to)
		{
			foreach (var pi in from.GetType().GetProperties())
			{
				var prop = to.GetType().GetProperties().SingleOrDefault(ti => ti.Name == pi.Name);
				if (prop != null)
				{
					prop.SetValue(to, pi.GetValue(from, new object[] { }), new object[] { });
				}
			}
		}
	}




Nick (ntcoding)

unread,
May 20, 2012, 2:57:20 PM5/20/12
to fubumv...@googlegroups.com
There's a mistake in that version on the variable view model, it should look like this:

if (failedValidationContext.IsForFailedValidation)
			{
				var viewModel = request.Get(GetViewModelType());
 
				MapProperties(failedValidationContext.InputModel, viewModel);
 
				request.Set(viewModel);
			}

Nick (ntcoding)

unread,
May 20, 2012, 3:08:06 PM5/20/12
to fubumv...@googlegroups.com
I did it!!!

Sorry for wasting anyone's time. But here's the solution - just use .AddAfter()

public class RemapFormValuesOnValidationFailurePolicy : IConfigurationAction
	{
		public void Configure(BehaviorGraph graph)
		{
			graph
				.Actions()
				.Where(b => b.InputType() != null
)
				.Each(chain => chain.AddAfter(new Wrapper(typeof(RemapFailedValidationFormValuesBehaviour<>).MakeGenericType(chain.OutputType().BaseType))));
		}
	}

On Sunday, May 20, 2012 7:45:48 PM UTC+1, Nick (ntcoding) wrote:

Jeremy D. Miller

unread,
May 20, 2012, 7:33:50 PM5/20/12
to fubumv...@googlegroups.com
Ok, so first, use the diagnostics to see what the behavior order is today.  If you need to force the behavior ordering, you want to use the ReorderBehaviorsPolicy.

ReorderBehaviorsPolicy has some helpers that might do the trick, but all you really need to care about is:


    public class ReorderBehaviorsPolicy : IConfigurationAction
    {
        public Func<BehaviorNode, bool> WhatMustBeBefore { get; set; }
        public Func<BehaviorNode, bool> WhatMustBeAfter { get; set; }

...........
    }

From your FubuRegistry, you can just do:  

Policies.Add(new ReorderBehaviorsPolicy(){
WhatMustBeBefore = something,
WhatMustBeAfter = something else
});

Just for fun, if this doesn't work for you, you might move to the version that's in the fubu master from the Fubu TeamCity nuget feed:   http://build.fubu-project.org/guestAuth/app/nuget/v1/FeedService.svc

I did a big cleanup of the FubuRegistry internals within the past month that guarantees that the reorder policies happen last. 

Jeremy D. Miller
The Shade Tree Developer
jeremy...@yahoo.com



From: Nick (ntcoding) <ntco...@gmail.com>
To: fubumv...@googlegroups.com
Sent: Sun, May 20, 2012 1:45:50 PM
Subject: [fubumvc] Ordering of Behaviours Applied By Policy
--
You received this message because you are subscribed to the Google Groups "FubuMVC Development Group" group.
To view this discussion on the web visit https://groups.google.com/d/msg/fubumvc-devel/-/8fF5Gaa8IaEJ.
To post to this group, send email to fubumv...@googlegroups.com.
To unsubscribe from this group, send email to fubumvc-deve...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/fubumvc-devel?hl=en.
Reply all
Reply to author
Forward
0 new messages